All rows tagged with a specific set of values

 ̄綄美尐妖づ 提交于 2019-12-08 14:06:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!