簡體   English   中英

mysql-根據另一列的值組合選擇一列

[英]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提交的數量。

首先,找到所有的比賽avidplistid然后檢查他們有不同的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.

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