Mysql 如何执行日期列表与现有表的左联接
我有一个表,它包含两列:日期和该日期产生的收入,但是,如果在某个日期没有产生收入,该日期将不会显示在表中,我想显示的是该日期和0作为收入 我尝试了一个在两个日期之间生成日期列表的查询,但是,我找不到如何将此查询与我的表左键联接,因为此日期生成器查询没有id 这是我的表查询:Mysql 如何执行日期列表与现有表的左联接,mysql,Mysql,我有一个表,它包含两列:日期和该日期产生的收入,但是,如果在某个日期没有产生收入,该日期将不会显示在表中,我想显示的是该日期和0作为收入 我尝试了一个在两个日期之间生成日期列表的查询,但是,我找不到如何将此查询与我的表左键联接,因为此日期生成器查询没有id 这是我的表查询: SELECT CAST(insertDate AS DATE) AS DATE, SUM(timeofcall)*10 as total_Revenue_Generated_from_CallBacks F
SELECT CAST(insertDate AS DATE) AS DATE,
SUM(timeofcall)*10 as total_Revenue_Generated_from_CallBacks
FROM callcompletion.tbl_calltracking
GROUP BY CAST(insertDate AS DATE);
结果->
DATE total_Revenue_Generated_from_CallBacks
2019-10-14 2200
2019-10-15 44000
2019-10-16 4400
2019-10-17 8800
2019-10-24 12100
这是日期生成器查询:
SELECT selected_date FROM (
SELECT adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date
from
(SELECT 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(SELECT 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(SELECT 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(SELECT 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(SELECT 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4
) v
WHERE selected_date BETWEEN '2019-10-14' AND '2019-10-24'
结果->
selected_date
2019-10-14
2019-10-15
2019-10-16
2019-10-17
2019-10-18
2019-10-19
2019-10-20
2019-10-21
2019-10-22
2019-10-23
2019-10-24
预期成果:
DATE total_Revenue_Generated_from_CallBacks
2019-10-14 2200
2019-10-15 44000
2019-10-16 4400
2019-10-17 8800
2019-10-18 0
2019-10-19 0
2019-10-20 0
2019-10-21 0
2019-10-22 0
2019-10-23 0
2019-10-24 12100
您需要决定您希望显示的最短和最长日期。除此之外,您只需将生成的日期与原始查询关联:
SELECT d.selected_date AS DATE, ifnull(SUM(c.timeofcall)*10,0) as total_Revenue_Generated_from_CallBacks
FROM (
SELECT adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date
FROM
(SELECT 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(SELECT 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(SELECT 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(SELECT 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(SELECT 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4
) as d
left join callcompletion.tbl_calltracking c on CAST(c.insertDate AS DATE) = d.selected_date
WHERE d.selected_date between '2019-10-14' and '2019-10-24'
GROUP BY d.selected_date;
请参见请参见:感谢您抽出时间回答,但由于某些原因,它不起作用/我将开始日期设置为“2019-10-14”,结束日期设置为“2019-10-24”,未经测试。现在应该可以工作了,因为列名现在是正确的,并且添加了ifnull。顺便说一句,您的日期生成子查询可能有点重,因为它生成了很多日期(从1970-01-01到2243-10-16)。缺少对数据库的引用(callcompletion)。你可以再试一次。如果你仍然有问题,请张贴你得到的错误。