mysql - How to ORDER BY a person's first_name but ALSO insert nicknames ASC -


here example data:

nickname    first_name    last_name =======     =======       =======                    charlie       smith                      beta          jones          alpha       chris         anderson       delta       nick          andrews        

this result want (first letters a, b, c, d)

  1. "alpha" chris anderson
  2. beta jones
  3. charlie smith
  4. "delta" nick andrews

my problem when run usual order nickname asc, first_name asc (b, c, a, d):

  1. beta jones
  2. charlie smith
  3. "alpha" chris anderson
  4. "delta" nick andrews

any ideas?

you want coalesce():

order coalesce(nickname, first_name) 

this assumes blank values null. if empty strings, then:

order coalesce(nullif(nickname, ''), first_name) 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -