Mysql统计结果为零值

发布于 2024-12-17 08:45:47 字数 638 浏览 3 评论 0原文

在mysql中,我得到了表“分数”,如下所示

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

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

In mysql, I got table "scores" as follow

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

Now when I am running query

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

I am getting result as

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

instead of

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

how can i display result with zero count, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李白 2024-12-24 08:45:47

这是一种简单的方法:

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

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

Here's one simple way:

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

This guarantees a date for everybody for every distinct date in the table

计㈡愣 2024-12-24 08:45:47

这是您计算与 where 条件匹配的行数时的预期结果。

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

它只选择一条记录,因此计数结果为 1。

如果您想对所有行进行计数,请不要使用 Where Clause 来限制数据,例如:-

SELECT date,count(id) as count FROM scores group by `date`;

同样,以下查询将返回 3作为计数..

SELECT date,count(id) as count FROM scores where id in (1,2,3) group by `date`;

It is The Expected Result as You Are Counting the number of rows that matches your where condition.

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

It selects only one record hence the result of count is 1.

If you want to count all the rows use don't restrict the data by using Where Clause e.g:-

SELECT date,count(id) as count FROM scores group by `date`;

Like wise the following query will return 3 as count..

SELECT date,count(id) as count FROM scores where id in (1,2,3) group by `date`;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文