说我有这样的查询
SELECT "contacts".name, "email_addresses".address
FROM "contacts"
LEFT JOIN "email_addresses" ON ("contacts"."id" = "email_addresses"."contact_id") WHERE (("contacts"."account_id" = 1) AND ("public" IS TRUE))
ORDER BY "contacts"."name", "email_addresses"."address"
某些结果可能包含空名称字段或空电子邮件地址,我想按字母顺序对display_name的计算属性进行排序,如果有的话,按名称排序,如果没有,则按电子邮件地址排序名字字段,所以我可以得到这样的结果:
name | email
====================================
null | aaron@gmail.com
Ben Jones | null
Colin Cowan | zach@gmail.com
等
答案 0 :(得分:0)
您可以使用COALESCE函数,该函数获取所提供参数的第一个非空值:
order by coalesce("contacts"."name","email_addresses"."address")