鉴于下表:
type | area | shelf
-----|------|------
1 | a | 5
2 | a | 5
2 | a | 6
1 | a | 7
2 | a | 7
1 | b | 3
类型2的区域/货架组合条目始终需要类型1的相应条目。类型1可以单独存在(例如,最后一行)。
如何找到孤立类型2行(类型为2的行而没有类型为1的对应行),例如第三行?
答案 0 :(得分:0)
你在找这个..
SELECT t2.*
FROM yourtable t1
RIGHT JOIN yourtable t2 ON t1.area = t2.area
AND t1.shelf=t2.shelf
AND t1.`type`=1
AND t2.`type`=2
WHERE t1.`type` IS NULL
答案 1 :(得分:0)
您可以使用聚合和select shelf, area
from t
group by shelf, area
having sum(type = 2) > 0 and -- at least one type 2
sum(type = 1) = 0; -- no type 1
子句执行此操作:
{{1}}