MySQL查询按多列排序

时间:2016-07-07 11:58:46

标签: mysql sql database sql-order-by

我有一行(firstname, lastname, phone, email)。我想要做的是一个首先按concat(firstname, lastname) asc排序的查询,其中firstnamelastname为空的其余行必须在按concat(firstname, lastname) asc排序之后排序根据{{​​1}}排序(如果phone/email为空排序phone,如果email为空排序email)。有什么建议可以帮到我吗?

2 个答案:

答案 0 :(得分:3)

您可以将这些规则放入order by。我认为这些是:

order by (firstname is null and lastname is null) asc,
         concat(firstname, lastname),
         coalesce(phone, email)

我不确定第一个条件应该是and还是or

答案 1 :(得分:1)

select * from table order by concat(firstname, lastname) asc,  (firstname is null and lastname is null) asc, coalesce(phone, email)