sql - Mysql recursive query in -
friends. mysql db has 4 tables products, product_properties, properties, property_values
products
|id|name| |1 |book1| |2 |book2| |3 |book3| ...
properties
|id|name| |1 |color| |2 |binding| ...
property_values
|id|property_id|value | |1 |1 | red | |2 |1 | orange| |3 |1 | green | |4 |2 | soft | |5 |2 | hard | product_properties
|id|product_id|property_id| value | |1 |1 |1 | 1 |2 |1 |2 | 4 |3 |2 |1 | 1 |4 |2 |2 | 4 |5 |3 |1 | 2 i need list of products have color red , binding soft. i'm confused.
select products.id products join product_properties on product_properties.product_id = products.id join properties on properties.id = product_properties.property_id join property_values on property_values.id = product_properties.value properties.name = 'color' , property_values.value = 'red' , properties.name = 'binding' , property_values.value = 'soft' ...not working...
you close:
select pp.id product_properties pp join properties p on p.id = pp.property_id join property_values pv on pv.id = pp.value (p.name = 'color' , pv.value = 'red') or (p.name = 'binding' , pv.value = 'soft') group pp.id having count(distinct p.name) = 2; note: removed products table, because not needed query.
this selects properties meet conditions. selects products have 2 matches.
Comments
Post a Comment