gpt4 book ai didi

mysql - 在 MySQL 中查找没有对应第二行的行

转载 作者:行者123 更新时间:2023-11-29 21:54:01 25 4
gpt4 key购买 nike

给出下表:

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 的行),例如第三行?

最佳答案

您可以使用聚合和 having 子句来做到这一点:

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

关于mysql - 在 MySQL 中查找没有对应第二行的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33357721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com