MySQL struggling with query in one to many relationship matching multiple conditions -


i have 2 tables set out follows:

products            product_attributes ==================  ======================================== | id | name      |  | id | product_id | attribute | value  | ==================  ======================================== | 1  | product 1 |  | 1  | 1          | size      | big    | | 2  | product 2 |  | 2  | 1          | colour    | red    | | 3  | product 3 |  | 3  | 2          | size      | medium | | 3  | product 3 |  | 4  | 2          | age_range | 3-5    | | .. | ...       |  | 5  | 2          | colour    | blue   | ==================  | 6  | 3          | size      | small  |                     | .. | ...        | ...       | ...    |                     ======================================== 

there potentially infinite amount of attributes product why kept in separate table.

i want able pull out distinct products match multiple (also infinite) attribute conditions cant think how without maybe using or condition , sort of count check of attributes matched. im sure isnt best way can help?!

for example find products have size = 'medium' , colour = 'blue' (this match product 2 in example above).

this relational division problem.

the way suggest count easiest in mysql

select product_id product_attributes pa (attribute='size' , value='medium') or (attribute='colour' , value='blue') group product_id having count(distinct concat(attribute,value) ) = 2 

there approach double not exists in linked article mysql not support ctes quite cumbersome.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -