我有两个表,艺术家和假名,艺术家属于假名
我想加入这两个表并在Artist.lastname上对结果进行排序。现在,如果他有假名,应使用假名,如下例所示,Balthus介于BACON和CHAGAL之间,即使他的姓氏以K开头
Artist.Lastname Pseudonym.name
BACON
KLOSSOWSKI Balthus
CHAGAL
CEZANNE
是否可以创建一个具有伪如果不为空或姓氏的虚拟列,然后对该列进行排序?
SELECT .....
FROM Artists as a
LEFT JOIN Pseudonyms as p ON (a.pseudonym_id = p.id)
ORDER BY the_virtual_column
答案 0 :(得分:2)
试试这个:
SELECT .....
FROM Artists as a LEFT JOIN Pseudonyms as p
ON (a.pseudonym_id = p.id)
ORDER BY COALESCE(p.name , a.Lastname)
答案 1 :(得分:2)
...ORDER BY COALESCE(p.name, a.Lastname)