If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Ordering with NULL values in a column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-01-09, 11:43
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Ordering with NULL values in a column

I'm trying to order the result of joining my Users table to my UserContacts table.

I get three columns:
idContacts
firstName
lastName

the field idContacts may or may not be NULL.

If idContacts is NULL, I want them to appear at the end of the list.

I tried doing:

Code:
ORDER BY idContacts DESC, lastName
But the first ordering rule screws with the second one - it does now list people who do have an idContacts first, but it orders them by idContacts rather than lastName.

I want to shove all the people without an idContacts to the bottom of the list, and then order by lastName.

Is this possible?
Reply With Quote
  #2 (permalink)  
Old 06-01-09, 11:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
ORDER 
    BY CASE WHEN idContacts IS NULL
            THEN 937 END
     , lastName
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-01-09, 11:54
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,260
..before you ask 937 is a magic number to Rudy (R937), it could be any number....
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 06-01-09, 13:10
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
That's awesome. Nice one. I have kept it as 937 in your honour
Reply With Quote
  #5 (permalink)  
Old 06-01-09, 19:30
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
I often wonder how much production code has 937 spuriously in it.

I know of a few chunks in prod at our place
__________________
George
Twitter | Blog
Reply With Quote
  #6 (permalink)  
Old 06-01-09, 22:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
i have some sql with the stooges --
Code:
ORDER
    BY CASE WHEN condition1 THEN 'Curly'
            WHEN condition2 THEN 'Larry'
            WHEN condition3 THEN 'Moe'
            ELSE 'Shemp' END
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On