Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
agriz
Forum Contributor
Posts: 106 Joined: Sun Nov 23, 2008 9:29 pm
Post
by agriz » Wed Feb 09, 2011 7:15 am
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
buckit
Forum Contributor
Posts: 169 Joined: Fri Jan 01, 2010 10:21 am
Post
by buckit » Wed Feb 09, 2011 8:53 am
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
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Wed Feb 09, 2011 1:37 pm
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).
There are 10 types of people in this world, those who understand binary and those who don't
Darhazer
DevNet Resident
Posts: 1011 Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria
Post
by Darhazer » Thu Feb 10, 2011 9:24 am
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);