选择问题

发布于 2024-12-02 20:15:31 字数 199 浏览 1 评论 0原文

我有一个表,其中的行有两列

A 1
A 2
B 1
B 3
C 1
C 2
C 3

,我只想从中获取这个 ID(a,b 或 c),它只有 2 行,值为 1,2,所以从这个表中我应该得到 a,因为 b 没有t 行包含 2,c 包含行包含 1 和 b,但也包含行包含 c..

获取该行的最简单方法是什么?

I have a table with rows with two columns

A 1
A 2
B 1
B 3
C 1
C 2
C 3

and I want to get from this only this ID(a,b or c) which has only 2 rows with value 1,2, so from this table I should get a, bacause b hasn't row with 2, and c has rows with 1 and b, but also has row with c..

What is the simplest way to get this row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随风而去 2024-12-09 20:15:31
SELECT col1
FROM YourTable
GROUP BY col1
HAVING COUNT(DISTINCT col2) =2 AND MIN(col2) = 1 AND MAX(col2) = 2

或者用另一种方式扩展到超过 2 个数字

SELECT col1
FROM   yourtable
GROUP  BY col1
HAVING MIN(CASE
             WHEN col2 IN ( 1, 2 ) THEN 1
             ELSE 0
           END) = 1
       AND COUNT(DISTINCT col2) = 2 
SELECT col1
FROM YourTable
GROUP BY col1
HAVING COUNT(DISTINCT col2) =2 AND MIN(col2) = 1 AND MAX(col2) = 2

Or another way extendible to more than 2 numbers

SELECT col1
FROM   yourtable
GROUP  BY col1
HAVING MIN(CASE
             WHEN col2 IN ( 1, 2 ) THEN 1
             ELSE 0
           END) = 1
       AND COUNT(DISTINCT col2) = 2 
秋千易 2024-12-09 20:15:31
select t1.col1 
from table as t1 
left join table as t2 on (t1.col1 = t2.col1) 
where t1.col2 = 1 and t2.col2 = 2;
select t1.col1 
from table as t1 
left join table as t2 on (t1.col1 = t2.col1) 
where t1.col2 = 1 and t2.col2 = 2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文