两个表的联合选择 * 但仅在某些列中没有重复项的情况下
Union of two tables selecting * but only where there are no duplicates in certain columns
如果我有两个 select 语句:
select * from x as a
union
select * from y as b
但我想合并 a 到 b,其中 b 没有与 a 相同的记录,但只在 a 和 b 列而不是 * 列,有没有办法做到这一点?
您可以使用 not exists
:
select *
from x
union all
select *
from y
where not exists (select 1 from x where x.a = y.a and x.b = y.b);
如果我有两个 select 语句:
select * from x as a
union
select * from y as b
但我想合并 a 到 b,其中 b 没有与 a 相同的记录,但只在 a 和 b 列而不是 * 列,有没有办法做到这一点?
您可以使用 not exists
:
select *
from x
union all
select *
from y
where not exists (select 1 from x where x.a = y.a and x.b = y.b);