【问题标题】:mysql - product attributes selectmysql - 产品属性选择
【发布时间】:2014-07-16 13:51:07
【问题描述】:

我有三个数据库表:

products
(
  product_id, name etc..
)

specifications
(
  id_specification, name 
)

product_has_specification
(
  id_specification, product_id, specification_value
)

假设我有一件尺寸为 M 的 T 恤 (product_id = 1),它具有蓝色和更多属性。

我已经尝试加入三个表,并且值 = m AND value =blue 等。但是它没有显示任何结果。

我也尝试过像这样的子查询:

select distinct products.* from products
join product_has_specification on products.id = product_has_specification.product_id
join specifications on product_has_specification.spec_id = pecifications.id_specification
where
(
(specifications.name = 'color' and product_has_specification.value='black')

)
 and products.id in 
 (
select products.id from products
left join product_has_specification on products.id = product_has_specification.product_id
left join specifications on product_has_specification.spec_id = specifications.id_specification
where

(specifications.name='size' and product_has_specification.value='s')

 ) 

如果产品颜色为“黑色”且尺寸为“s”,则此选择有效,但是如果我有更多属性,假设 X 选择将太长。

选择会是什么样子? 有没有其他解决办法?

编辑:

我找到了解决办法。它很丑,但它可以完成工作。但是,如果一个产品有 10 个规格,那么查询很长。

        SELECT products.*
        FROM products
        left JOIN product_has_specification ON products.id = product_has_specification.product_id
        left JOIN specifications ON product_has_specification.id_specifiction = specifications.id_specification
        WHERE products.id in (

                SELECT products.id
                FROM products
                left JOIN product_has_specification ON products.id = product_has_specification.product_id
                left JOIN specifications ON product_has_specification.id_specifiction = specifications.id_specification
                where   (product_has_specification.value = 'm') and products.id in 

                    (
                        SELECT products.id
                        FROM products
                        left JOIN product_has_specification ON products.id = product_has_specification.product_id
                        left JOIN specifications ON product_has_specification.id_specifiction = specifications.id_specification
                        where   (product_has_specification.value = 'black') and products.id in 
                        (
                                SELECT products.id
                                FROM products
                                left JOIN product_has_specification ON products.id = product_has_specification.product_id
                                left JOIN specifications ON product_has_specification.id_specifiction = specifications.id_specification
                                where   (product_has_specification.value = 'test') 
                        )

                    )

        )
        group by products.id

将此查询调整为您想要的任意数量的规范。如果有人有更好的解决方案,请发布。

【问题讨论】:

    标签: php mysql sql codeigniter select


    【解决方案1】:

    您可以使用 EXISTS 和 AND 运算符来选择所有颜色为黑色和尺寸为 s 的产品:

    select distinct products.* from products
    WHERE
    EXISTS
    (select 1 from product_has_specification join specifications on product_has_specification.spec_id = specifications.id_specification
    where specifications.name = 'color' and product_has_specification.value='black' and products.products.id = product_has_specification.product_id) 
    and
    EXISTS
    (select 1 from product_has_specification join specifications on product_has_specification.spec_id = specifications.id_specification
    where specifications.name='size' and product_has_specification.value='s' and products.products.id = product_has_specification.product_id) 
    

    【讨论】:

    • 已经试过这个方法了。不工作。假设我有另一种黑色的产品,它也会返回。感谢您的回答
    • 修复了查询,看看吧。
    • 还是不行。它显示所有产品。谢谢你的尝试。我找到了一个解决方案,它很丑,但它可以完成工作。我会更新答案。
    • 检查这个小提琴:sqlfiddle.com/#!2/c804f4/1 你会看到它不会显示所有产品,而只会显示那些有规格的产品。
    • 我不敢相信我第一次错过了什么。感谢您花时间制作小提琴。奇迹般有效。此外,可以删除 where 子句中的“specifications.name”。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 2015-03-24
    • 2017-03-17
    • 2018-03-17
    相关资源
    最近更新 更多