2013-03-06 88 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