[英]Cannot check if all records have a specific values in field
I'm trying to check if a specific list of matches
have as status: 3 and 5, so suppose I have a list of matches
like this: 我正在尝试检查特定的
matches
列表是否具有状态:3和5,所以假设我有一个类似的matches
列表:
id | round_id | status
1 28 3
2 28 3
3 28 5
4 28 5
the query result should return true
because all the matches
available have as status 3
or 5
. 查询结果应返回
true
因为所有可用的matches
状态均为3
或5
。 I wrote this query: 我写了这个查询:
SELECT (SELECT COUNT(DISTINCT `status`) FROM `match` WHERE round_id = 28 AND (`status` = 3 OR`status` = 5)) = 1 AS result
but this will return 0
但这将返回
0
You can do: 你可以做:
select ( count(*) = sum(status in (3, 5)) ) as flag_3_5
from matches
where round_id = 28;
You can do this for all rounds by using group by round_id
. 您可以使用
group by round_id
在所有回合中执行此group by round_id
。
MySQL treats boolean expressions as numbers in a numeric context, with true being "1" and false being "0". MySQL将布尔表达式视为数字上下文中的数字,其中true为“ 1”,false为“ 0”。 So,
sum(status in (3, 5))
counts the number of rows with these two statuses. 因此,
sum(status in (3, 5))
计算具有这两种状态的行数。 The comparison checks to see if these are all rows. 比较将检查这些是否全部都是行。
You can use exists
: 您可以使用
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;
Try this: 尝试这个:
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.