我不确定正确的术语是否正确描述了这个问题所以我的搜索由于常用术语而得出了很多结果。
我有一张看起来像这样的表: id status
1 finished
1 finished
1 finished
1 skipped
2 skipped
2 skipped
2 finished
2 finished
3 finished
3 skipped
3 finished
3 skipped
4 skipped
4 skipped
4 skipped
4 skipped
5 finished
5 finished
5 skipped
5 skipped
6 finished
6 finished
6 skipped
6 skipped
如果包含该id的所有4行中的值在第二列(状态)中“跳过”,我想从第一列(id)返回id。如果只有1或2或3包含跳过,则不感兴趣。
因此,对于上表,结果将是:4
答案 0 :(得分:2)
您可以使用group by
和having
:
select id
from t
group by id
having min(status) = max(status) and min(status) = 'skipped';
这不会检查恰好四行。如果这很重要,请在and count(*) = 4
子句中添加having
。