簡體   English   中英

無法檢查所有記錄是否在字段中都有特定值

[英]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狀態均為35 我寫了這個查詢:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM