【问题标题】:Create the query for multiple product property为多个产品属性创建查询
【发布时间】:2013-11-22 16:40:59
【问题描述】:

昨天我遇到了一个问题:

High availability Search implementation in PHP+MySQL

实施melc的解决方案后,我需要更改我的查询,我遇到了这个问题:

所以我有:

product:
id int(10) unsigned not null primary key auto_increment
name varchar(200),
price float(5,2),
type int(10)

attribute:
id int(10)
name varchar(100) /*e.g. red, small, large, green, metal, plastic etc...*/

product_attribute:
attribute_id int(10)
product_id int(10)

所以我在“属性”表中拥有所有自定义属性,例如颜色、尺寸、金属/塑料类型等。到目前为止,我有超过 500 和 50 种类型(大小、颜色、形状等...)

所以,如果我想获得我做的所有红色产品:

select * from product_attribute
where attribute_id = 4 (for reg)

现在,如果我想要所有与此匹配的产品:红色和蓝色、小号、塑料

我试着做:

select * from product_attribute
where attribute_id = 4 and attribute_id = 5 and attribute_id = 10 and attribute_id = 38

但这没有返回,所以我尝试了:

select * from product_attribute
where attribute_id = 4 OR attribute_id = 5 OR attribute_id = 10 OR attribute_id = 38

但后来我得到的属性与我想要的不对应!

我能做什么?我的数据库表错了吗?

【问题讨论】:

    标签: mysql sql database-design


    【解决方案1】:

    您可以使用 UNION:

    如果他们选择 2 个属性:

    SELECT * FROM product_attribute where attribute_id = 4
    UNION
    SELECT * FROM product_attribute where attribute_id = 8
    

    或 3:

    SELECT * FROM product_attribute where attribute_id = 4
    UNION
    SELECT * FROM product_attribute where attribute_id = 8
    UNION
    SELECT * FROM product_attribute where attribute_id = 37
    

    你明白了。

    这将得到所有结果,现在您必须按产品 ID 对它们进行分组,所以

    SELECT * FROM (
        SELECT * FROM product_attribute where attribute_id = 4
        UNION
        SELECT * FROM product_attribute where attribute_id = 8
        UNION
        SELECT * FROM product_attribute where attribute_id = 37  
    ) AS allItems
    INNER JOIN product ON (allItems.product_id = product.id)   
    GROUP BY allItems.product_id
    /* ORDER BY price  */
    

    这将返回您需要的产品列表

    【讨论】:

      【解决方案2】:

      如果您想查找既是红色又是塑料的产品,您必须使用JOINS,因为我猜您的 Product_Attribute 表会将多个属性保存为单独的行... 例如,如果您需要购买红色和塑料的产品

      SELECT a.product_id, a.attribute_id, b.attribute_id
      FROM product_attribute a
          JOIN product_attribute b JOIN a.product_id = b.product_id AND a.attribute_id <> b.attribute_id
      WHERE a.attribute_id = 4 AND b.attribute_id = 10
      

      这将为您提供具有 2 个属性的产品,类似地,如果您需要具有 2 个以上属性的产品,您将不得不使用更多的 JOIN...

      【讨论】:

        【解决方案3】:

        如果你不想要一个老鼠窝:

        SELECT
          *
        FROM product p
        WHERE NOT EXISTS (
          SELECT 4  UNION
          SELECT 5  UNION
          SELECT 10 UNION
          SELECT 38
          EXCEPT
          SELECT attribute_id
          FROM product_attribute
          WHERE product_id = p.product_id
        )
        

        对于每个产品,取所需的属性集,减去该产品的属性集,如果没有剩余,则该产品必须具有所有所需的属性。

        此外,您可以使用其他查询或表值参数,而不是使用 select-union 来声明所需的属性集。这样,您可以比较任意数量的属性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-02-03
          • 2014-02-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-19
          • 2021-06-05
          相关资源
          最近更新 更多