[英]Cannot check if all records have a specific values in field
我正在尝试检查特定的matches
列表是否具有状态:3和5,所以假设我有一个类似的matches
列表:
id | round_id | status
1 28 3
2 28 3
3 28 5
4 28 5
查询结果应返回true
因为所有可用的matches
状态均为3
或5
。 我写了这个查询:
SELECT (SELECT COUNT(DISTINCT `status`) FROM `match` WHERE round_id = 28 AND (`status` = 3 OR`status` = 5)) = 1 AS result
但这将返回0
你可以做:
select ( count(*) = sum(status in (3, 5)) ) as flag_3_5
from matches
where round_id = 28;
您可以使用group by round_id
在所有回合中执行此group by round_id
。
MySQL将布尔表达式视为数字上下文中的数字,其中true为“ 1”,false为“ 0”。 因此, sum(status in (3, 5))
计算具有这两种状态的行数。 比较将检查这些是否全部都是行。
您可以使用exists
:
select distinct m.round_id, (case when exists (select 1
from match m1
where m1.round_id = m.round_id and
m1.status in (3,5)
)
then 'true' else 'false'
end) as result
from match m
where round_id = 28;
尝试这个:
select round_id from matches
where status in (3, 5)
group by round_id
having count(distinct status) > 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.