我是 sql 新手,遇到了一个小问题。 我有一个这样的查询:
select cust_id, count(1) from all_customers
group by cust_id
having count(1)>4;
那个查询给了我想要的结果。
我需要对 all_customers
表中的所有客户进行新的查询,并排除我刚刚从上面的查询中得到的结果。
我试着做这样的事情:
select * from all_customers
where cust_id NOT IN
(
select cust_id, count(1) from all_customers
group by cust_id
having count(1)>4
)
但我收到错误消息太多值
。
我做错了什么?
最佳答案
您应该去掉NOT IN
子句中的aggregated 列。原因是您只比较 cust_id
列。另请注意,使用 NOT IN
时,子查询应始终返回单列。
select *
from all_customers
where cust_id NOT IN
(
select cust_id
from all_customers
group by cust_id
having count(1)>4
)
关于sql - 使用 NOT IN 提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12603378/