首页 > 解决方案 > SQL 合并 - 在结果输出中填充合并值的其他方法?

问题描述

我想加入两个表

have1
key ...
a   ...
b   ...
c   ...

have2
key ...
a   ...
c   ...
d   ...

并获得类似的输出

want
key ...
a   ...
b   ...
c   ...
d   ...

我知道

create table want as 
  select coalesce(a.key, b.key) as key, ..., 
  from have1 a full join have2 b 
  on a.key=b.key;

会给我输出,但有其他选择吗?我想要更简洁易读的代码,并且加入 3 或 4 个条件似乎需要大量文本才能实现所需的输出(例如,相对于 SAS 数据步骤)。

标签: sqlsasfull-outer-join

解决方案


您可以使用union all

select h1.*
from have1 h1
union all
select h2.*
from h2
where not exists (select 1 from have1 h1 where h1.key = h2.key);

推荐阅读