左联接后合并列

时间:2019-04-18 08:06:41

标签: sql

如何根据哪一列不为null来将3个返回值列合并为一列?

我的查询是:

SELECT val1,val2,val3
FROM db.table1 t1
LEFT JOIN db.table2 t2 ON t2.pk = t1.t2_fk
LEFT JOIN db.table3 t3 ON t3.pk = t1.t3_fk

1 个答案:

答案 0 :(得分:0)

使用coalesce()

SELECT coalesce(val1,val2,val3) as va
FROM db.table1 t1
left join db.table2 t2
on t2.pk = t1.t2_fk
left join db.table3 t3
on t3.pk = t1.t3_fk