SQL 选择其中仅包含特定值的行

发布于 2024-11-09 17:06:16 字数 704 浏览 0 评论 0原文

我有一个这样的表,

Col 1 Col 2 Col 3
1       A     1
2       A     2
3       B     1
4       C     1
5       C     2
6       D     1

如何仅获取 Col 3 = 1 的唯一行?

我想要获取第 3 行和第 6 行(第 2 列分别 = B 和 D)。我不想要 A 或 C,因为它们也有 Col 3 = 2。

我尝试过以下方法:

select col 2 from table group by col 2having count(col 3) = 1

但这只会显示 Col 2 的结果,所以我不确定 Col 是否3 内容 = 1 或否。

编辑:抱歉,也许我没有清楚地表达我的问题。我想要获取第 2 列中仅包含第 3 = 1 且仅 1 的所有行。

因此,如果我尝试 WHERE Col 3= 1,它将返回 4 行,因为 A 有 1 行。但由于 A 也有一行 Col 3 = 2,所以我不希望这样,C 也是如此。从这个示例表中,我希望最终结果只显示 2 行,B 和 D。

我的示例表只是一个示例,我实际上有大约 5000 行要过滤,否则我会按照你们的建议进行操作:)

I have a table as such

Col 1 Col 2 Col 3
1       A     1
2       A     2
3       B     1
4       C     1
5       C     2
6       D     1

How do I only get unique rows which have Col 3 = 1?

I want to get rows 3 and 6 (Col 2 = B and D respectively). I do not want A nor C since they have Col 3 = 2 as well.

I've tried something along the lines of:

select col 2 from table group by col 2 having count(col 3) = 1

But that only brings up Col 2 for results so I'm uncertain if Col 3 contents = 1 or not.

EDIT: Sorry guys maybe I've not worded my question clearly. I want to get all of the rows of Col 2 which contain only Col 3 = 1 AND ONLY 1.

So if I tried WHERE Col 3= 1, it would return 4 rows because A has 1. But since A also has a row where Col 3 = 2, I do not want that, same for C. From this example table, I would want the end result to only show 2 rows, B and D.

My example table is an example, I actually have about 5000 rows to filter through, otherwise I'd do as you guys have suggested :)

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

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

发布评论

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

评论(5

燕归巢 2024-11-16 17:06:16
SELECT col2
FROM your_table
GROUP BY col2
HAVING MAX(col3) = 1 AND MIN(Col3) = 1

或者

SELECT a.col2 
FROM your_table a
WHERE a.col3=1 AND NOT EXISTS(SELECT *
                              FROM your_table b 
                              WHERE a.col2=b.col2 AND b.col3<>1)
SELECT col2
FROM your_table
GROUP BY col2
HAVING MAX(col3) = 1 AND MIN(Col3) = 1

Or

SELECT a.col2 
FROM your_table a
WHERE a.col3=1 AND NOT EXISTS(SELECT *
                              FROM your_table b 
                              WHERE a.col2=b.col2 AND b.col3<>1)
吲‖鸣 2024-11-16 17:06:16

您可能正在寻找的是 WHERE 条款。

SELECT * FROM YouTable WHERE col3 = 1 AND col2 in ('B','D');

What you are probably looking for is WHERE clause.

SELECT * FROM YouTable WHERE col3 = 1 AND col2 in ('B','D');
弥繁 2024-11-16 17:06:16
;with T ([Col 1], [Col 2], [Col 3]) as
(
select 1,       'A',     1 union all
select 2,       'A',     2 union all
select 3,       'B',     1 union all
select 4,       'C',     1 union all
select 5,       'C',     2 union all
select 6,       'D',     1
)


select *
from T
  left outer join 
    (
      select distinct [Col 2] 
      from T
      where [Col 3] <> 1
    ) as T2
  on T.[Col 2] = T2.[Col 2]
where T.[Col 3] = 1 and
      T2.[Col 2] is null
;with T ([Col 1], [Col 2], [Col 3]) as
(
select 1,       'A',     1 union all
select 2,       'A',     2 union all
select 3,       'B',     1 union all
select 4,       'C',     1 union all
select 5,       'C',     2 union all
select 6,       'D',     1
)


select *
from T
  left outer join 
    (
      select distinct [Col 2] 
      from T
      where [Col 3] <> 1
    ) as T2
  on T.[Col 2] = T2.[Col 2]
where T.[Col 3] = 1 and
      T2.[Col 2] is null
蓝天白云 2024-11-16 17:06:16

确切地知道你想要得到什么有点困难,但这是我最好的猜测:

SELECT * FROM theTable WHERE col2 NOT IN
  (SELECT col2 FROM theTable WHERE col3 <> 1)

It's a bit hard to know exactly what you're trying to get, but this is my best guess:

SELECT * FROM theTable WHERE col2 NOT IN
  (SELECT col2 FROM theTable WHERE col3 <> 1)
韬韬不绝 2024-11-16 17:06:16
SELECT * FROM #temp t1
WHERE EXISTS 
(
select Col2 from #Temp t2 
WHERE t2.Col2 = t1.Col2
group by col2 
having count(col3) = 1
)

使用 MS SQL2008 和以下内容进行了测试(因此,如果我的答案不正确,可能会阻止其他人测试他们的答案......):

CREATE TABLE #temp 
( 
    Col1 INT, 
    Col2 CHAR(1),
    Col3 INT
)

INSERT INTO #Temp    
    (Col1, Col2, Col3)
SELECT    1,'A',1
UNION
SELECT    2,'A',2
UNION
SELECT  3,'B', 1
UNION 
SELECT 4,'C',1
UNION 
SELECT 5,'C',2
UNION 
SELECT 6,'D',1

SELECT * FROM #temp t1
WHERE EXISTS 
(
select Col2 from #temp t2 
WHERE t2.Col2 = t1.Col2
group by col2 
having count(col3) = 1
)

DROP TABLE #temp
SELECT * FROM #temp t1
WHERE EXISTS 
(
select Col2 from #Temp t2 
WHERE t2.Col2 = t1.Col2
group by col2 
having count(col3) = 1
)

tested with MS SQL2008 and the following (so if my answer is not the correct one it may halp others test theirs...):

CREATE TABLE #temp 
( 
    Col1 INT, 
    Col2 CHAR(1),
    Col3 INT
)

INSERT INTO #Temp    
    (Col1, Col2, Col3)
SELECT    1,'A',1
UNION
SELECT    2,'A',2
UNION
SELECT  3,'B', 1
UNION 
SELECT 4,'C',1
UNION 
SELECT 5,'C',2
UNION 
SELECT 6,'D',1

SELECT * FROM #temp t1
WHERE EXISTS 
(
select Col2 from #temp t2 
WHERE t2.Col2 = t1.Col2
group by col2 
having count(col3) = 1
)

DROP TABLE #temp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文