我写的第一个问题是:
select t.groupNum from table1 t
JOIN table1 s
on t.groupNum=s.groupNum
and t.id!=s.id
and t.bool='true' and s.bool='true';
此查询选择在组中至少包含两个“true”的组。对吗?
我需要选择所有“true”值为零的组(只有t.bool=“false”,这是“true”和“false”两个值中的第二个可能值)。
有什么帮助吗?
最佳答案
您可以使用group by
和having
来执行此操作。
select groupNum
from table1
group by groupNum
having sum(bool='true') = 0
MySQL将条件视为布尔值,当条件为True时返回
1
,否则返回0
。