2016-03-01 149 views
0

我喜歡做一個嵌套選擇多達5個過濾器,我仍然在3個過濾器。這是代碼。MySQL嵌套多達5個過濾器

SELECT * FROM anvariety_trait 
INNER JOIN 
(SELECT variety_id FROM anvariety_trait 
    INNER JOIN (SELECT variety_id FROM anvariety_trait 
        WHERE 
         trait_id = 1 and trait_value = 1) T2 USING (variety_id) 
        where 
         trait_id = 2 AND trait_value = 1) T3 USING (variety_id) 
         INNER JOIN tblVariety USING (variety_id) 
          WHERE 
          trait_id = 3 and trait_value = 1; 
+0

我敢打賭,沒人理解你的問題。請添加更多詳情。 – RubahMalam

+0

好吧,它就像excel的過濾器函數,您可以選擇某個值,然後它只顯示具有選定值的行。 –

回答

0

我能夠通過將子查詢,以添加過濾器 「INNER JOIN(SELECT variety_id FROM anvariety_trait WHERE trait_id = 1和trait_value = 1)T1 USING(variety_id)」。我做這件事情要容易得多,也不復雜,但現在它對我很有用。

SELECT * FROM anvariety_trait 
INNER JOIN 
(SELECT variety_id FROM anvariety_trait 
INNER JOIN (SELECT variety_id FROM anvariety_trait 
INNER JOIN(SELECT variety_id FROM anvariety_trait 
INNER JOIN(SELECT variety_id FROM anvariety_trait 
WHERE 
trait_id = 1 and trait_value = 1) T1 USING (variety_id) 
WHERE 
trait_id = 2 and trait_value = 1) T2 USING (variety_id) 
WHERE 
trait_id = 3 and trait_value = 1) T3 USING (variety_id) 
WHERE 
trait_id = 4 AND trait_value = 1) T4 USING (variety_id) 
INNER JOIN tblVariety USING (variety_id) 
WHERE 
trait_id = 6 and trait_value = 1;