Table 1 :
A
123
231
232
333
Table 2 :
1 | 2
123 981
231 999
OUTPUT Table:
A
981
999
232
333
我想从2个表中获取输出表。基本上我想写一个我无法完成的案例条件。
答案 0 :(得分:2)
此处您不需要case
表达式。您可以使用left join
,只需coalesce
结果:
SELECT COALESCE(b.id, a.id)
FROM a
LEFT JOIN b ON a.id = b.id
答案 1 :(得分:1)
以下两个值是等价的:
COALESCE(b.id, a.id)
或
case when t2.A is null then t1.A else t2.B end
或
ISNULL(b.id, a.id)
他们的表现没有差别。只有语法。
答案 2 :(得分:0)
select case when t2.A is null then t1.A else t2.B end
from table1 t1
left join table2 t2
on t1.A=t2.A