2013-03-06 87 views
1

我希望能夠流利的MySQL能夠幫助我。我正在嘗試對select進行選擇,但查詢似乎並不想完成。任何幫助將不勝感激。單個查詢中的多個選擇

SELECT 
    product as pid, 
    leg_name as name, 
    dimensions as dims 
FROM 
    pmaint 
WHERE 
    product in (
     SELECT product 
     FROM qb_export_items_attributes 
     WHERE attribute_name = 'Z' 
     AND product in (
       SELECT product 
       FROM pmainT 
       WHERE type_ID = (
         SELECT ID 
         FROM type 
         WHERE SOFTCARTCATEGORY = 'End Table Legs' 
        ) 
       ) 
     AND attribute_value <= 3.5 
    ) 
+1

1)你是什麼意思的「不想完成?」 2)你有什麼努力使它工作? (例如,您是否嘗試過每個人都選擇瞭解其中一個是否已損壞?) – 2013-03-06 05:31:27

+3

爲什麼子查詢不加入? – 2013-03-06 05:32:28

+2

@AndrewMcGivery如果我正在喝咖啡,我會把它吐在我的屏幕上 – Phil 2013-03-06 05:36:54

回答

1

嘗試使用內部聯接,而不是IN子

UPD:我已經按照你對此有何評論編輯此查詢。第一個JOIN子查詢輸出全部爲product,其中兩個屬性都存在且爲true。

SELECT 
    pmaint.product as pid, 
    pmaint.leg_name as name, 
    pmaint.dimensions as dims 
FROM 
    pmaint 
JOIN (select product from qb_export_items_attributes 
     where ((attribute_name = 'Z') and (attribute_value <= 3.5)) 
      OR 
      ((attribute_name = 'top_square') and (attribute_value >= 4)) 
     GROUP BY product HAVING COUNT(*)=2 
    ) 

     t1 on (pmaint.product=t1.product) 
JOIN type on (pmaint.type_ID=type.ID) 
WHERE 
     type.SOFTCARTCATEGORY = 'End Table Legs' 
+0

Valex,真是太棒了!這很好用! – 2013-03-06 05:52:33

+0

Valex,我忘了添加一個其他組件,我希望你可以幫助... 如果t1.attribute_name ='Z',那麼t1.attribute_value <= 3.5。 如果t1.attribute_name ='top_square',那麼t1.attribute_value> = 4。 這兩個條件必須正確才能顯示單個產品ID。 t1的數據看起來像這樣。 'PRODUCT | attribute_name | attribute_value 1 | Z | 3.5 1 | top_square | 3 2 | Z | 2 2 | top_square | 5 3 | Z | 5 3 | top_square | 4' 因爲Z <= 3.5 AND top_square> = 4,所以我們應該只匹配產品ID 2. 再次感謝您的幫助至目前 – 2013-03-06 06:15:38

+0

替換 - 帶換行符。然後數據看起來更適合於表格格式。 - 產品| attribute_name | attribute_value - 1 | Z | 3.5 - 1 | top_square | 3 - 2 | Z | 2 - 2 | top_square | 5 - 3 | Z | 5 - 3 | top_square | 4 - 我們應該只匹配產品ID 2,因爲Z <= 3.5 AND top_square> = 4。 – 2013-03-06 06:23:18