ordering by 2 columns

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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

ordering by 2 columns

Post by John Cartwright »

I am doing a search where I need to search by 2 different columns..

`accounts`.`status` and `accounts`.`id`

Now status is an enum (paid, free, or expired) and id is autoincremented.. when I try to put both in them in the order by clause it will not work as expected, expected being

Code: Select all

paid 100
paid 82
paid 70

free 99
free 25
free 21
So in plain english, status paid comes up first (all I'm concerned about) and then it orders by descending Id's.. anyone know how I can accomplish this
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's the result you're getting?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

order by status, id
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you create a pseudo-field (concatenated field of status and id) to sort by? Or what about ORDER BY status DESC, ID DESC?
Post Reply