简体   繁体   中英

mysql - product attributes select

I have three database tables:

products
(
  product_id, name etc..
)

specifications
(
  id_specification, name 
)

product_has_specification
(
  id_specification, product_id, specification_value
)

Let's say I have a t-shirt (product_id = 1) that is size M has color blue and more attributes to it.

I have tried joining the three tables and have the value = m AND value =blue etc.. however it does not display any results.

I have also tried with sub queries like this one:

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')

 ) 

This select works if a product have color 'black' and size 's' however if I have more attributes let's say X the select will be too long.

How would the select look like? Is there any other solution to this?

EDIT:

I have found a solution. it is ugly but it does the job. However, If a product, have, let's say 10 specifications the query is very long.

        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

adapt this query to as many specification you'd like. If someone has a better solution, please post it.

you can use EXISTS and AND operator to select all products which has color black and size 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM