在MySQL查询中搜索多个属性(使用工作示例)

时间:2017-07-19 12:38:26

标签: mysql

我有以下表格

表:产品

+------------+--------------------+
| product_id | name               |
+------------+--------------------+
| 1          | samsung galaxy s8  |
| 2          | apple iphone 7     |
+------------+--------------------+

表格:属性

+--------------+--------------------+
| attribute_id | name               |
+--------------+--------------------+
| 1            | brand              |
| 2            | color              |
+--------------+--------------------+

表格: attribute_values

+--------------------+--------------+------------+---------------------+
| attribute_value_id | attribute_id | product_id | value               |
+--------------------+--------------+------------+---------------------+
| 1                  | 1            | 1          | samsung             |
| 2                  | 2            | 1          | blue                |
| 3                  | 1            | 2          | apple               |
| 4                  | 2            | 2          | red                 |
+--------------------+--------------+------------+---------------------+

我有以下查询:

查询1(工作!)

    SELECT
                p.product_id                AS product_id,
                p.name                      AS product_name,
                v.value                     AS attribute_value,
                a.attribute_id              AS attribute_id,
                a.name                      AS attribute_name,
                c.name                      AS attributes_category_name
    FROM
                products p
    LEFT JOIN
                attribute_values v USING (product_id)
    LEFT JOIN
                attributes a USING (attribute_id)
    WHERE
                p.product_id IN (
                    SELECT 
                       p.product_id 
                    FROM 
                       products p 
                    LEFT JOIN 
                      attribute_values v USING (product_id) 
                    LEFT JOIN  
                      attributes a USING (attribute_id) 
                    WHERE  
                      (a.name = 'brand' AND (v.value = 'samsung'))
                )

查询2(不工作!)

    SELECT
                p.product_id                AS product_id,
                p.name                      AS product_name,
                v.value                     AS attribute_value,
                a.attribute_id              AS attribute_id,
                a.name                      AS attribute_name,
                c.name                      AS attributes_category_name
    FROM
                products p
    LEFT JOIN
                attribute_values v USING (product_id)
    LEFT JOIN
                attributes a USING (attribute_id)
    WHERE
                p.product_id IN ( 
                    SELECT   
                      p.product_id  
                    FROM   
                      products p  
                    LEFT JOIN   
                      attribute_values v USING (product_id)  
                    LEFT JOIN   
                      attributes a USING (attribute_id)  
                    WHERE   
                      (a.name = 'brand' AND (v.value = 'samsung'))   
                      AND (a.name = 'color' AND (v.value = 'blue'))  
                   )

如您所见,2个查询之间的区别在于WHERE-clausule。

Query 1:
--------
p.product_id IN (
    SELECT  
        p.product_id 
    FROM  
        products p 
    LEFT JOIN  
        attribute_values v USING (product_id) 
    LEFT JOIN  
        attributes a USING (attribute_id) 
    WHERE  
        (a.name = 'brand' AND (v.value = 'samsung')) 
   )

Query 2:
--------
p.product_id IN (
    SELECT 
        p.product_id 
    FROM  
        products p 
    LEFT JOIN  
        attribute_values v USING (product_id) 
    LEFT JOIN  
        attributes a USING (attribute_id) 
    WHERE  
        (a.name = 'brand' AND (v.value = 'samsung'))  
        AND (a.name = 'color' AND (v.value = 'blue')) 
   )

我在查询一中只搜索品牌>三星,在查询2中为品牌>三星和颜色>蓝色。

有谁知道为什么我的第二个查询不起作用?

2 个答案:

答案 0 :(得分:1)

查看WHERE查询中的second条件:

 WHERE  
  (a.name = 'brand' AND (v.value = 'samsung'))  
  AND (a.name = 'color' AND (v.value = 'blue'))

这种情况最终类似于:

WHERE a.name = 'brand' AND v.value = 'samsung'
  AND a.name = 'color' AND v.value = 'blue'

对于任何记录,这将NEVERTRUE

答案 1 :(得分:1)

我仍然没有足够的声誉来评论stackoverflow,但我认为表c永远不会被声明,所以我将其删除。这是我使用您的代码解决问题的方法。

SELECT
          p.product_id                AS product_id,
          p.name                      AS product_name,
          v.value                     AS attribute_value,
          a.attribute_id              AS attribute_id,
          a.name                      AS attribute_name/*,
          c.name                      AS attributes_category_name*/
FROM
          products p
LEFT JOIN
          attribute_values v USING (product_id)
LEFT JOIN
          attributes a USING (attribute_id)
WHERE 
          p.product_id IN ( 
              SELECT   
                p2.product_id  
              FROM   
                products p2  
              LEFT JOIN   
                attribute_values v USING (product_id)  
              LEFT JOIN   
                attributes a USING (attribute_id)  
              WHERE   
                a.name = 'brand' AND v.value = 'samsung' and p2.product_id IN (
                  SELECT   
                    p3.product_id  
                  FROM   
                    products p3  
                  LEFT JOIN   
                    attribute_values v USING (product_id)  
                  LEFT JOIN   
                    attributes a USING (attribute_id)
                  WHERE a.name = 'color' AND v.value = 'blue'
              )
          )

所以基本上你应该首先制作一个接一个的过滤器,而不是同时制作两个过滤器,没有属性值为蓝色和三星。希望这有帮助