我想在MySQL中找出两个表的不匹配记录。
发布于 2018-01-17 14:42:38
您可以使用NOT EXISTS谓词。假设你想模仿:
select a.c1, a.c2 from a
except
select b.c1, b.c2 from b这可以表示为:
select distinct a.c1, a.c2
from a
where not exists (
select 1 from b
where b.c1 = a.c1
and b.c2 = a.c2
)其他选项是使用左联接并检查null:
select distinct a.c1, a.c2
from a
left join b
on a.c1 = b.c1
and a.c2 = b.c2
where b.c1 is null请注意,如果两个关系中都存在相同的元组并包含null,则这与EXCEPT不同。设a= { (1,1),(2,2),(1,null)},b= {(1,1),(1,空)}
select a.c1, a.c2 from a
except
select b.c1, b.c2 from b
(2,2)在某种意义上,EXCEPT认为null等于null,而如果a.c2或b.c2为null,则谓词a.c2 = b.c2计算为null,因此NOT EXISTS计算为false。
select distinct a.c1, a.c2
from a
where not exists (
select b.c1, b.c2 from b where a.c1 = b.c1 and a.c2 = b.c2
)
(2,2),(1,null)LEFT JOIN的行为类似于NOT EXISTS:
select distinct a.c1, a.c2
from a
left join b
on a.c1 = b.c1 and a.c2 = b.c2
where b.c1 is null
(2,2),(1,null)https://dba.stackexchange.com/questions/195592
复制相似问题