问题
To be more specific, for a many to many relation between Questions and Tags as sql server tables (including the helper table QuestionsWithTags), is needed a query/sp that returns all the questions that have the following tag set "t1", "t2" and "t3". The discriminator there is not any of the tags, but all of them, no less, no more than that.
Thanks in advance.
回答1:
You can solve this using aggregation and a having clause:
select questionid
from QuestionsWithTags qwt
group by questionid
having count(distinct tag) = 3 and
count(distinct case when tag in ('t1', 't2', 't3') then tag end) = 3;
来源:https://stackoverflow.com/questions/24813847/all-rows-tagged-with-a-specific-set-of-values