gpt4 book ai didi

mysql - 分组依据和具有 "Unknown column"

转载 作者:行者123 更新时间:2023-11-29 05:06:01 24 4
gpt4 key购买 nike

我有一个这样的表:

-----------------------------------------
id | user_id | done | created_at
-----------------------------------------
1 | 5 | 0 | 2017-12-23 23:28:00
1 | 2 | 1 | 2017-12-23 23:28:00
1 | 5 | 1 | 2017-12-23 23:28:00
1 | 10 | 0 | 2017-12-23 23:28:00
1 | 7 | 0 | 2017-12-23 23:28:00
1 | 5 | 1 | 2017-12-23 23:28:00

我想得到的是至少有一个 done 为 0 的用户数(注意 user_id 列值可以重复多次)

我试过这个:

select count(*) as sum from users group by user_id having done=0

但它说 Unknown column 'done' in 'having clause'

如何使用 MySQL 获取正确的数据?

最佳答案

要获得至少有一个 done = 0 的用户,您可以使用以下内容

select count(*) as sum 
from users
group by user_id
having sum(done=0) > 0

DEMO

获取至少有一条记录的所有用户的数量,其中 done = 0

select count(distinct user_id) 
from users
where done=0;

DEMO

关于mysql - 分组依据和具有 "Unknown column",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48059211/

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