Thread: NULLS first ...
View Single Post

   
  #2 (permalink)  
Old 02-28-2008, 08:30 AM
Nicholas Sherlock
 
Posts: n/a
Default Re: NULLS first ...

Ralph wrote:
> Hi
>
> Is there a way to make NULLs appear first in this example:
>
> SELECT login_time FROM my_table GROUP BY id ORDER BY login_time DESC
>
> This gives me the list starting from most recent login and ending with
> NULL values. Is there a way to make NULLs appear first keeping the times
> data sorted like they are now.


You can do it like this:

SELECT login_time, IF(login_time IS NULL,1,0) AS sortkey FROM my_table
ORDER BY sortkey DESC, login_time DESC

There are probably better ways, too.

Cheers,
Nicholas Sherlock

--
http://www.sherlocksoftware.org
Reply With Quote