I have a common problem to be sure. I'd like to make a query that finds an entity that has "n" tags. So in the simplest case, we find all the entities that have the tag "hey". In a more complex case, we find all the entities that have all the tags "hey", "hi" and "howdy".
It seems that I have to join to the tag table 3 times, and and thus create 3 different aliases. In the abstract case, I will have to make N different aliases. Is there a simpler way to achieve this?
The reason I am asking is that I need to write a query that not only does this for tags, but for a variety of things. So I am basically going to join NxM aliases... which is going to suck to write (and tune) the query.
Help?
EDIT:
Nevermind. I found the solution:
select distinct g.id, g.description
FROM gallery g
inner join gallery_to_tag g2t_0
on g2t_0.gallery_id = g.id
inner join tag t_0
on t_0.id = g2t_0.tag_id and t_0.term = 'hi'
inner join gallery_to_tag g2t_1
on g2t_1.gallery_id = g.id
inner join tag t_1
on t_1.id = g2t_1.tag_id and t_1.term = 'hey'