mySQL: Querying one-to-many -table? -


what appropriate way query products specific property in following database design one-to-many approach?

i guess should doing following: select (*) productproperties property = 'weight' , value = '10'

but if need products has both weight = 10 & color = blue in same query?

example of database design:

table: products

------------------------ id    | name     |  price ------------------------ 0     | myname   |  100 1     | myname2  |  200 

table: productproperties

------------------------------------------------ product  | property     |  value ------------------------------------------------ 0        | weight       |  10 1        | weight       |  20 1        | color        |  blue 

what if need products has both weight = 10 & color = blue in same query?

one option:

select product, name   products inner join productproperties     on (products.id = productproperties.product)  (property = 'weight' , value = '10')     or (property = 'color' , value = 'blue')  group product, name having count(1) = 2 

another option subqueries:

select id, name   products p  exists (          select 1            productproperties pp1           p.id = pp1.product              , pp1.property = 'weight'             , value = '10'        )    , exists (          select 1            productproperties pp2           p.id = pp2.product              , pp2.property = 'color'             , value = 'blue'        ) 

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? -