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:
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
. 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
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
.
MySQL treats boolean expressions as numbers in a numeric context, with true being "1" and false being "0". So, sum(status in (3, 5))
counts the number of rows with these two statuses. The comparison checks to see if these are all rows.
You can use 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.