如何仅根据 1 个条件选择 ID

发布于 2025-01-11 19:58:27 字数 368 浏览 1 评论 0原文

在显示的数据集中,我希望隔离或显示仅在工作篮 a、b 或 c 中但不在 d、e 或 f 中的 ID。我希望 ID 111222 符合该标准。我尝试过这个 SQL,但没有成功。有什么建议吗?

select id
from table
where workbasket in ('a','b','c') and workbasket not in ('d','e','f')

Workbasket  ID
a           111222
a           123456
b           987654 
c           112333
d           123456 
e           987654
f           112333

In the dataset shown, I am looking to isolate or show the ID that is only in workbaskets a,b or c but not in d,e or f. I would expect the ID of 111222 to fit that criteria. I have tried this SQL with no luck. Any suggestions?

select id
from table
where workbasket in ('a','b','c') and workbasket not in ('d','e','f')

Workbasket  ID
a           111222
a           123456
b           987654 
c           112333
d           123456 
e           987654
f           112333

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

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

发布评论

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

评论(3

亚希 2025-01-18 19:58:27
select id
from table
where workbasket in ('a','b','c')

EXCEPT

select id
from table
where workbasket in ('d','e','f')
select id
from table
where workbasket in ('a','b','c')

EXCEPT

select id
from table
where workbasket in ('d','e','f')
眼泪也成诗 2025-01-18 19:58:27

您还可以使用 NOT EXISTS 子查询

select id
from table t1
where workbasket in ('a','b','c') 
and NOT EXISTS 
(
 select *
 from table t2
 where t1.id = t2.id
 AND t2.workbasket in ('d','e','f') 
)

You can also use NOT EXISTS subquery

select id
from table t1
where workbasket in ('a','b','c') 
and NOT EXISTS 
(
 select *
 from table t2
 where t1.id = t2.id
 AND t2.workbasket in ('d','e','f') 
)
毁梦 2025-01-18 19:58:27
WITH CTE(Workbasket, ID) AS
(
   SELECT 'A',123456 UNION ALL
   SELECT 'A',111222 UNION ALL
   SELECT 'B',987654 UNION ALL
   SELECT 'C',112333 UNION ALL
   SELECT 'D',123456 UNION ALL
   SELECT 'E',987654 UNION ALL
   SELECT 'F',112333
)
SELECT C.ID
FROM CTE AS C
GROUP BY C.ID
HAVING MAX(C.Workbasket) IN('A','B','C')
WITH CTE(Workbasket, ID) AS
(
   SELECT 'A',123456 UNION ALL
   SELECT 'A',111222 UNION ALL
   SELECT 'B',987654 UNION ALL
   SELECT 'C',112333 UNION ALL
   SELECT 'D',123456 UNION ALL
   SELECT 'E',987654 UNION ALL
   SELECT 'F',112333
)
SELECT C.ID
FROM CTE AS C
GROUP BY C.ID
HAVING MAX(C.Workbasket) IN('A','B','C')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文