在SQL Server中选择具有一组值的行(在连接[和]中的多行中)(select rows with a set of values (in multiple rows in Conjunction

编程入门 行业动态 更新时间:2024-10-27 18:33:07
在SQL Server中选择具有一组值的行(在连接[和]中的多行中)(select rows with a set of values (in multiple rows in Conjunction [And]) in sql Server)

假设我有一张桌子,如下所示:

id............value ``````````````````` A............1 A............2 A............3 B............1

等等。

所以,如果我想选择值为1或2或3的行,我这样做:

select id From table where value in (1,2,3)

但是,我想要做的是,选择id与AND而不是OR。 我想要它

所以如果我想选择Id值为1,2和3的行(在这种情况下,结果= A),我该如何选择?

谢谢! (我试图自己做,没有运气,我试图在谷歌搜索,我意识到我的力量甚至知道如何说出问题!所以如果这里的问题不符合我的实际疑问,请随时更改它)

Suppose I have a table like:

id............value ``````````````````` A............1 A............2 A............3 B............1

and so on.

so, If I want to select rows with Value either 1 OR 2 OR 3, I do this:

select id From table where value in (1,2,3)

But, what I want to do is, select id with AND instead of OR. I want it like

so If I want to select rows where Id has value 1, 2 AND 3, (in this case, resultant = A), how do I select that?

Thanks! (I tried to do it myself, no luck, I tried to search on google, I realized I dint even know how to phrase the question! so if the question here doesnt agree with my actual doubt, feel free to change it)

最满意答案

select id from MyTable where value in (1, 2, 3) group by id having count(distinct value) = 3 select id from MyTable where value in (1, 2, 3) group by id having count(distinct value) = 3

更多推荐

本文发布于:2023-07-07 09:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061400.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:select   rows   多行中   SQL   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!