SQL database tag request -
i have photo table looks this:
id title rating photopath 1 myself 7.0 /photopath1.jpg 2 cat 8.0 /photopath2.jpg 3 dog 6.0 /photopath3.jpg 4 girlfriend 5.0 /photopath4.jpg
and tag table:
id tag_name photo_id 1 selfie 1 2 sun 1 3 nature 2 4 relax 2 5 loyal 3 6 journal 3 7 selfie 4 8 sun 4 9 problems 4
and want call photos have tags "selfie" , "sun". how do that?
select p.* photo p join (select p.id photo p join tag t on p.id = t.photo_id t.tag_name in ('selfie', 'sun') group p.id having count(*) = 2) x on p.id = x.id
fiddle: http://sqlfiddle.com/#!2/5c95c2/2/0
output:
| id | title | rating | photograph | |----|------------|--------|------------------| | 1 | myself | 7 | /photograph1.jpg | | 4 | girlfriend | 5 | /photograph4.jpg |
Comments
Post a Comment