mysql - 产品属性选择

时间:2014-05-27 15:11:18

标签: php mysql sql codeigniter select

我有三个数据库表:

products
(
  product_id, name etc..
)

specifications
(
  id_specification, name 
)

product_has_specification
(
  id_specification, product_id, specification_value
)

让我们说我有一件T恤(product_id = 1),大小M的颜色为蓝色,属性更多。

我尝试过加入这三个表,并且值= 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')

 ) 

如果产品颜色为“黑色”,则此选项有效。和尺寸'但是如果我有更多的属性,那就说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

将此查询与您喜欢的规范相匹配。如果有人有更好的解决方案,请发布。

1 个答案:

答案 0 :(得分:0)

您可以使用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)