Bluehost.com Web Hosting $6.95

NULLS first ...

This is a discussion on NULLS first ... within the MySQL Database forums, part of the Database Forums category; Hi Is there a way to make NULLs appear first in this example: SELECT login_time FROM my_table GROUP BY id ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-06-2006
Ralph
 
Posts: n/a
Default NULLS first ...

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.
Reply With Quote
  #2 (permalink)  
Old 11-06-2006
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
  #3 (permalink)  
Old 11-06-2006
Ralph
 
Posts: n/a
Default Re: NULLS first ...

Nicholas Sherlock wrote:
> 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
>

Thank you that helped :)
Reply With Quote
  #4 (permalink)  
Old 11-06-2006
Harald Fuchs
 
Posts: n/a
Default Re: NULLS first ...

In article <eim9cv$rhi$1@lust.ihug.co.nz>,
Nicholas Sherlock <N.sherlock@gmail.com> writes:

> 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.


Yep:

SELECT login_time
FROM my_table
GROUP BY id
ORDER BY login_time IS NOT NULL, login_time DESC
Reply With Quote
  #5 (permalink)  
Old 11-07-2006
Nicholas Sherlock
 
Posts: n/a
Default Re: NULLS first ...

Harald Fuchs wrote:
> ORDER BY login_time IS NOT NULL, login_time DESC


Neat, thanks, I'll remember that one.

Cheers,
Nicholas Sherlock

--
http://www.sherlocksoftware.org
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:15 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0