Page 1 of 1

Order by

Posted: Wed Feb 09, 2011 7:15 am
by agriz

Code: Select all

Select * from tbl where id in (2,4,3,6,9,35);
Id is the autoincrement id.
For the above query i am getting answers in sorted.
How to get without sorting.?

Right now i am getting ,
2
3
4

But i want

2
4
3
6

Re: Order by

Posted: Wed Feb 09, 2011 8:53 am
by buckit
Create a columnin the table called "sortorder" and put in incremental numbers. then ORDER BY the sortorder column

id | sortorder
=================
2 | 1
4 | 2
3 | 3
6 | 4

Re: Order by

Posted: Wed Feb 09, 2011 1:37 pm
by VladSun
agriz, you are doing something wrong. ID should not be used for ordering. It's purpose is to assure field/column unique value for a record. Ordering is something that should be "implemented" with other field(s) (take a look at buckit's suggestion).

Re: Order by

Posted: Thu Feb 10, 2011 9:24 am
by Darhazer

Code: Select all

Select * from tbl where id in (2,4,3,6,9,35) order by field(id, 2,4,3,6,9,35);