[英]mysql - select a column based on a combination of values from another column
您好,我需要得到plistid
根據給定的組合avid
。 例如,我提交了1和4 avid
的組合,它應該返回plistid
。另一個示例是,當我提交了2和5 avid
的組合時,它應該返回plistid
。如果我提交了1和3,它應該返回plistid
。由於它們具有相同的attributeid
因此不返回任何內容。
我如何在mysql中生成它,這是我的表
注意:avid可以是1,2,3或4個數字的組合,具體取決於avid提交的數量。
首先,找到所有的比賽avid
的plistid
然后檢查他們有不同的attributeid
。 您可以使用聚合來完成此操作,這是“集內查詢”的一種變體:
select plistid
from t
group by plistid
having sum(avid = 1) > 0 and
sum(avid = 4) > 0 and
count(distinct case when avid in (1, 4) then attributeid end) = 2
中前兩個條件having
條款在說什么avid
你想秒。 他們正在計算avid
出現在其中一個值上的次數,每個值僅在找到至少一個值時才返回true。 最后是說您需要在行上使用不同的屬性ID。
嘗試:
select plistid
from t
where avid in (1,4)
group by plistid
having count(distinct avid) =2
那這個呢?
SELECT t1.plistid
FROM t t1
JOIN t t2
ON t1.attributeid != t2.attributteid
AND t1.plistid = t2.plistid
WHERE
t1.avid = 1
AND t2.avid = 4
試試這個:
select a.plistid
from your_table a, your_table b
where a.plistid = b.plistid
and a.avid = <first param>
and b.avid = <second param>
and a.attributeid <> b.attributeid;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.