I have a band/event database that is setup with two fields that I want to order by. However, there is a little oddity with this order by that is causing me issues. I want to order by last name if that field isn't empty, if it is order by first name. I'm having a problem explaining it, so I can show you example data and what I have done so far. (Artists typically use both fields, and a band will just use first name) table: first_name, last_name 'Bryan, 'Adams' 'Belafonte, '' 'Berlin','' My Order by: ORDER BY LENGTH(IFNULL(last_name, ''))=0 asc, last_name, first_name The problem with this code is it shows all bands together, then all of the artists. Each are working from the stand point all bands are sorted alphabetically and all artists are sorted alphabetically, but I would like them combined. Thank you for your help.
Could you not join the Last_Name & First_Name (in that order) and order by that? Thus if there is no last name it will only be the first name ORDER BY (Last_Name + First_Name) asc Code (markup):
Check out the IFNULL function of Mysql: http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull Using that your ORDER BY clause would be: ORDER BY IFNULL(last_name, first_name) ASC
Thanks for the idea. I ended up using a combination of your concept and another process: select if(last_name ='', first_name, concat(last_name,' , ',first_name) as band_name order by band_name I tried that originally but wasn't coming out exactly how I wanted.