How to best use SQL to find common IDs that match multiple WHERE clauses -


so i'm starting learn sql, , hit upon following problem. suppose have table 3 columns so:

 id    | property_name    | property_value 1     | color            | "blue" 1     | size             | "large" 2     | color            | "red" 3     | color            | "orange" 3     | size             | "small" 4     | color            | "blue" 4     | size             | "large" ... 

now, suppose want find ids have color=blue , size=large (aka. id 1 , 4), how best this. best way came following, seems clunky...

select id propertytable id in (         select id propertytable         property_name='color' , property_value='blue' ) , (property_name='size' , property_value='large') 

thank :)

edit: forgot add preformat tags example table text. did so.

how self join?

select t1.id propertytable t1 join propertytable t2 on t1.id = t2.id         t1.propertyname = 'color' , t1.propertyvalue = 'blue'     ,         t2.propertyname = 'size' , t2.propertyvalue = 'large' 

here sqlfiddle


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -