Order by

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Order by

Post 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
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: Order by

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Order by

Post 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).
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Order by

Post 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);
Post Reply