gpt4 book ai didi

mysql - 按日期计数和分组应在没有值时返回 0

转载 作者:可可西里 更新时间:2023-11-01 08:22:15 25 4
gpt4 key购买 nike

我正在尝试报告我们每天进行的采访次数。

所以我有一张采访表比如

interviewid,staffid,date,comments...

和一个日期引用表,其中包含从 2005 年到 2020 年的所有日期。具有一个名为 ref 的日期字段。

我的查询是:

SELECT count(*) as cnt FROM `interviews` 
right JOIN `dateRef` ON `date` = ref where type = 2
and date > date_sub(now(),interval 7 day) group by date_format(ref,'%Y-%m-%d')

是否可以很好地显示我们所做的采访,但当我们没有进行任何采访时则不能...

例如这返回:

1
2
4

但它应该返回

0
1
0
2
0
4
0

编辑:

显然问题出在 where 子句上,因为如果我删除它,查询就可以正常工作...

最佳答案

尝试替换为

right join

代替

left join

另一个变化是

where type = 2 

where type = 2 OR type IS NULL

SO 最后的查询

SELECT count(*) as cnt FROM `interviews` 
right JOIN `dateRef` ON `date` = ref where (type = 2 OR type is null)
and date > date_sub(now(),interval 7 day) group by date_format(ref,'%Y-%m-%d')

关于mysql - 按日期计数和分组应在没有值时返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4322652/

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