gpt4 book ai didi

Mysql 计数结果为零

转载 作者:可可西里 更新时间:2023-11-01 07:19:07 26 4
gpt4 key购买 nike

在 mysql 中,我得到如下表“scores”

Id  Date                Name        score
1 2011-08-24 00:00:00 sharique 10
2 2011-08-24 00:00:00 joe 11
3 2011-08-24 00:00:00 vijay 5
4 2011-08-25 00:00:00 sharique 0
5 2011-08-25 00:00:00 joe 11

现在当我运行查询时

SELECT date,count(id) as count FROM scores where `name` = 'vijay' group by `date`;

我得到的结果是

date                 count
2011-08-24 00:00:00, 1

代替

date                 count
2011-08-24 00:00:00, 1
2011-08-25 00:00:00, 0

请问如何显示零计数的结果?

最佳答案

这是一种简单的方法:

SELECT s2.date, count(s1.id) as count
FROM (select distinct `date` from scores) s2
left join scores s1
on s1.`date` = s2.`date`
and s1.`name` = 'vijay'
group by 1

这保证了表中每个不同日期的每个人的日期

关于Mysql 计数结果为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8212174/

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