我正在尝试检查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
答案 0 :(得分:4)
您可以这样做:
select ( count(*) = sum(status in (3, 5)) ) as flag_3_5
from matches
where round_id = 28;
您可以使用group by round_id
进行所有回合。
MySQL在数字上下文中将布尔表达式视为数字,true为“ 1”,false为“ 0”。因此,sum(status in (3, 5))
计算具有这两种状态的行数。比较将检查这些是否全部都是行。
答案 1 :(得分:0)
您可以使用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;
答案 2 :(得分:0)
尝试一下:
select round_id from matches
where status in (3, 5)
group by round_id
having count(distinct status) > 1