[英]How to find rows missing by every group
I have two tables: 我有两个表:
Input: 输入:
A:
ID col
1 a
1 b
1 c
2 a
2 b
3 x
4 y
B
ID col
1 a
1 b
2 a
I want to for every ID in B, find rows in A but not in B by every ID. 我想为B中的每个ID在A中找到行,但不按每个ID在B中找到行。
Output: 输出:
ID Col
1 c
2 b
What I tried: 我试过的
select * from a left join b on a.id = b.id where b.id is null
select * from a left join b on a.id = b.id where b.id is null
select * from a except select * from b
but not sure how to modify it. 但不确定如何修改。
Assuming you want the values in A
for which there are records in B
with the same ID
, but not the same col
, you could do: 假设您想要
A
的值,而B
中的记录具有相同的ID
,但不具有相同的col
,则可以执行以下操作:
select
a.ID,
a.col
from A
left join B
on b.ID = a.ID and b.col = a.col
where A.ID in (select distinct ID from B) -- B contains this `ID` somewhere...
and B.ID is null -- ...but not with the same `col`
Using a combination of exists
and not exists
. 使用
exists
和not exists
的组合。
select *
from a
where exists (select 1 from b where a.id=b.id) --id check
and not exists (select 1 from b where a.id=b.id and a.col=b.col) -- col check
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.