簡體   English   中英

MySQL的情況下,當計數不同

[英]MySQL case when count distinct inside where

我正在嘗試執行以下代碼但出現錯誤

WHERE (case when (Count(distinct idc) >= 2) then idw NOT LIKE '%_pronto_' ELSE TRUE end) 

有時我會偽造'idc'值,如果是的話,我想排除idw列具有'% pronto '這樣的字符串的行

這些是查詢的結果,我需要在外部查詢中對其進行過濾:

# userid, idw,        uniq, idc
193,   q3470_pronto_, 0,   3470
193,   q3470_next_,   1,   3470
193,   q3496_alt_1_,  10,  34961
193,   q3498_alt_3_,  11,  34983
193,   q3499_alt_2_,  12,  34992

在上面的這種情況下,我想排除第1行,因為我有多個idc = 3470,第1行有idw,如% pronto

我需要它,因為有時我有uniq idc,並且需要保留pronto

這樣的事情可以為您工作:

select *
from t
where find_in_set(uniq, (
            select group_concat(uniqs)
            from (
                select group_concat(case when t2.idw like '%\_pronto\_' then t2.uniq end) as uniqs
                from t t2
                group by userid,
                    idc
                having count(*) > 1
                ) t
            )) = 0;

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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