ORDER by NULLs first then values

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
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

ORDER by NULLs first then values

Post by someberry »

Hello, quick question which I'm not sure is even possible, but I would have a table like with two columns, an ID and a UNIX timestamp when the event is complete, so the table looks something like:

Code: Select all

id          date
1           1234567890
2           NULL
3           6789012345
4           NULL
5           8901234567
However, I would like the query to order the rows with 'NULL' in the date column first and then order the remaining rows by the date DESC, so:

Code: Select all

id          date
2           NULL
4           NULL
1           1234567890
3           6789012345
5           8901234567
Anyone know if this is possible?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: ORDER by NULLs first then values

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: ORDER by NULLs first then values

Post by someberry »

Perfect! Thanks!
Post Reply