gpt4 book ai didi

sql - WHERE ALL 没有按预期工作

转载 作者:行者123 更新时间:2023-12-04 17:10:27 24 4
gpt4 key购买 nike

如果他有 A 类和 B 类信用卡,我想查找员工的详细信息

表结构如 {empid, ccno, cctype} 并假设 empid 'e1' 具有所有卡类型。

我试过类似的东西

select * from test where cctype = all('A', 'B') and empid = 'e1'

但这并没有返回任何行。

你能解释一下为什么我错了吗?任何帮助表示赞赏。提前致谢。

最佳答案

ALL有效地扩展为 bool 值 and .您的查询等同于:

select * 
from test
where cctype = 'A'
and cctype = 'B'
and empid = 'e1'

作为cctype不能同时是 A B没有行被返回。

相等检查 ( = ) 对 ALL 很少有用, 比较运算符( > , < , <> )更有用。

如果你想找到同时具有这两种类型的人,你必须对一个键使用聚合或分析函数:

select empid
from test
where cctype in ('A', 'B')
group by empid
having count(distinct cctype) = 2

select *
from ( select t.*
, count(distinct cctype) over (partition by empid) as ct
from test t
where cctype in ('A', 'B')
)
where ct = 2

关于sql - WHERE ALL 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47991490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com