SQL将所有两个结果合并并从不同结果将值放入不同的列中

时间:2018-06-28 17:33:25

标签: sql sql-server sql-server-2008

我有下面的ID,税项和保险列的税表

enter image description here

类似地,我的保险表具有相同的列

enter image description here

现在,我希望查询结果可以同时提取两个表中的所有ID,仅提取税表中的Tax列和保险表中的Insurance列,并使其他值如下所示

enter image description here

如何在SQL中实现此目标?

1 个答案:

答案 0 :(得分:2)

您需要FULL OUTER JOIN

select coalesce(t.id, i.id) as id, t.tax, i.insurance 
from tax t full outer join 
     insurance i
     on t.id = i.id
order by 1;
相关问题