Retrieve a record that has 2 specific features in SQL -
i have following database of pictures (a picture can have multiple features):
table picture ------------- id name 1 mona lisa 2 scream table features ------------- id name pictureid (fk picture) 1 portrait 2 2 expressive 2 3 big 2 4 small 1 5 expressive 1 5 big 1
i'd wish query retrieves pictures portrait , big, (so result in case "scream"). i've come query retrieve it, i'm not sure if prettiest , efficient way it. here's proposal:
select * picture o (select count(*) feature c o.id = c.pictureid , c.name '%portrait%') >= 1 , (select count(*) feature c o.id = c.pictureid , c.name '%big%') >= 1
in case, have go through features table twice (which, personal taste, find "ugly").
thanks , regards
select picture.name picture join features on picture.id=features.pictureid features.name in('portrait','big') group features.pictureid having count(distinct features.id)>=2
Comments
Post a Comment