我试图查询两个表并返回一个结果集,该结果集包含表A中的所有值,并且仅包含表B中不包含在表A中的值。
Table A Table B
ID Name ID Name
A John C Drew
B Jacob D Shane
C Nancy
基于ID列,结果集应为:
ID Name
A John
B Jacob
C Nancy
D Shane
由于表A的ID为C,因此我将从结果集中的表B中排除IDC。
有人知道我将如何开始实现这一目标吗?
答案 0 :(得分:2)
您可以使用union all
和not exists
来做到这一点:
select a.id, a.name
from a
union all
select b.id, b.name
from b
where not exists (select 1 from a where a.id = b.id);