我想查找员工的详细信息,如果他有A和B类型的信用卡.
表结构类似{empid, ccno, cctype},假设empid'e1'具有所有卡类型.
我试过类似的东西
select * from test where cctype = all('A', 'B') and empid = 'e1'
Run Code Online (Sandbox Code Playgroud)
但这并没有返回任何行.
你能解释我为什么错吗?任何帮助表示赞赏.提前致谢.
ALL有效地扩展到布尔值and.您的查询相当于:
select *
from test
where cctype = 'A'
and cctype = 'B'
and empid = 'e1'
Run Code Online (Sandbox Code Playgroud)
由于cctype不能同时返回A ,因此 B不返回任何行.
相等检查(=)是具有很少有用ALL,比较运算符(>,<,<>)是更为有用.
如果要查找具有这两种类型的个人,则必须对密钥使用聚合或分析函数:
select empid
from test
where cctype in ('A', 'B')
group by empid
having count(distinct cctype) = 2
Run Code Online (Sandbox Code Playgroud)
要么
select *
from ( select t.*
, count(distinct cctype) over (partition by empid) as ct
from test t
where cctype in ('A', 'B')
)
where ct = 2
Run Code Online (Sandbox Code Playgroud)