MySQL query, intersection on many-to-many relationship -
does have idea/solution how achieve this?
situation is: have tables 'releases' , 'ntags', related via 'releases_ntags' (containing 'release_id' , 'ntag_id')
and fetch results releases via ntag's 'slug'.
i manage have semi-working:
sql
select r.id, r.name releases r left join ntags_releases rt on rt.release_id = r.id join ntags nt on nt.id = rt.ntag_id , (nt.name = ('tag_1') or nt.name = ('tag_2')) group r.id, r.name
so far good, gives me releases "tag_1" plus releases "tag_2" (and off course both tags).
but need intersection of tags, say:
"releases 'tag_1' and 'tag_2'"
so tried with:
... , (nt.name = ('tag_1') , nt.name = ('tag_2')) ...
but leads in empty result. have idea how achieve this? don't know how go further on , appreciate input!
thx
you can demand 2 distinct ntags present in having
clause:
select r.id, r.name releases r join ntags_releases rt on rt.release_id = r.id join ntags nt on nt.id = rt.ntag_id nt.name in ('tag_1', 'tag_2') group r.id, r.name having count(distinct nt.name) = 2
Comments
Post a Comment