gpt4 book ai didi

Mysql - 排除计数>值但显示所有行

转载 作者:行者123 更新时间:2023-11-29 07:37:00 25 4
gpt4 key购买 nike

我有一个名为 customer_numbers 的表,如下所示:

ID   customer      number

1 1 10001
2 2 10002
3 2 10003
4 3 10004
5 3 10005
6 3 10006
7 3 10007

我想要一个产生以下结果的查询。 IE。仅排除那些只有一个号码的客户。

ID   customer      number

2 2 10002
3 2 10003
4 3 10004
5 3 10005
6 3 10006
7 3 10007

我试过这样的查询:

SELECT ID,customer,number from customer_numbers GROUP BY number HAVING count(*) > 1

添加 GROUP BY 子句列出所有数字,但 count(*) 始终为 1。没有该子句意味着没有列出所有数字,只有一个结果。

正确的查询是什么?

最佳答案

您只需要按客户分组

select * from customer_numbers
where customer in(SELECT customer from customer_numbers GROUP BY customer HAVING count(*)>1)

关于Mysql - 排除计数>值但显示所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48346461/

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