单个查询中的多个选择

时间:2013-03-06 05:29:31

标签: mysql

我希望精通MySQL的人能够帮助我解决这个问题。我正在尝试对select上的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 个答案:

答案 0 :(得分:1)

尝试使用INNER JOIN而不是IN子查询

UPD:我根据您的评论编辑了此查询。第一个JOIN子查询输出所有product,其中两个属性都存在且为真。

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'