用一组特定值标记的所有行 [英] All rows tagged with a specific set of values

查看:37
本文介绍了用一组特定值标记的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更具体地说,对于作为 sql server 表(包括帮助表 QuestionsWithTags)的 Questions 和 Tags 之间的多对多关系,需要一个查询/sp 返回具有以下标记集t1"的所有问题、t2"和t3".那里的鉴别器不是任何标签,而是所有标签,不多也不少.

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.

提前致谢.

推荐答案

您可以使用聚合和 sharing 子句来解决这个问题:

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;

这篇关于用一组特定值标记的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆