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