SQL: ORDER BY two columns intermixed, not priority based

前端 未结 4 1396
一整个雨季
一整个雨季 2021-01-13 11:13

I\'m using mySQL. I have to order names of Contacts by Lastname but in the case that there is no last name, I order by firstname.

This looks like:

OR         


        
4条回答
  •  轮回少年
    2021-01-13 11:47

    Try using Coalesce
    Note: this would require you not to store empty last names using an empty string (ie "")

    ORDER BY Coalesce(LastName, FirstName)
    

    As Suggested in the Comments By adding FirstName to the order By list again you will properly order two people with the same lastName. Here is an example.

    ORDER BY Coalesce(LastName, FirstName), FirstName
    

提交回复
热议问题