首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NOT IN抽取数据

问使用NOT IN抽取数据
EN

Stack Overflow用户
提问于 2012-09-26 21:55:24
回答 2查看 44关注 0票数 2

我是sql的新手,遇到了一个小问题。我有一个查询是这样的:

代码语言:javascript
复制
select cust_id, count(1) from all_customers
group by cust_id
having count(1)>4;

该查询给出了我想要的结果。

我需要对all_customers表中的所有客户进行新的查询,并排除从上面的查询中获得的结果。我试着这样做:

代码语言:javascript
复制
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
)

但是我得到了错误消息too many values。我做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-26 21:57:36

您应该去掉NOT IN子句中的聚合列。原因是您只比较cust_id列。还要注意,在使用NOT IN时,子查询应该始终返回单列。

代码语言:javascript
复制
select * 
from all_customers
where cust_id NOT IN 
      (
         select cust_id 
         from all_customers
         group by cust_id
         having count(1)>4
      )
票数 6
EN

Stack Overflow用户

发布于 2012-09-26 21:58:25

子查询中有太多列,请尝试:

代码语言:javascript
复制
select * from all_customers
where cust_id NOT IN 
(select cust_id
 from all_customers
 group by cust_id
 having count(1)>4)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12603378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档