数据库表:
我有MySQL查询:
select calendar.datefield AS date,
( SELECT IFNULL(SUM(visits),0)
FROM link_stats
WHERE link_stats.link_id = '1'
AND statDate = date
) AS visits,
( SELECT Round(IFNULL(SUM(leads * (rate/1000)),0),3)
FROM link_stats
WHERE link_stats.link_id = '1'
AND statDate = date
) AS cash,
( SELECT IFNULL(SUM(leads),0)
FROM link_stats
WHERE link_stats.link_id = '1'
AND statDate = date
) AS leads
from `calendar`
where `calendar`.`datefield` between '2016-05-10' AND '2016-05-11'
我尝试使用空白天生成每个链接的统计信息报告。
示例:
2016年1月11日至2016年1月20日期间,链接没有日期为12-01-2016的记录。
答案 0 :(得分:0)
构建一个包含所有可能日期的表格。称之为TableOfDates并有一列:datefield
然后重新排列这样的查询:
select d.datefield AS date,
ls.visits, ls.cash, ls.leads
from TableOfDates d
LEFT JOIN `calendar` AS c ON c.datefield = d.datefield
LEFT JOIN
( SELECT statDate,
IFNULL(SUM(visits),0) AS visits,
Round(IFNULL(SUM(leads * (rate/1000)), 0),3) AS cash,
IFNULL(SUM(leads),0) AS leads
FROM link_stats
WHERE ls.link_id = 1
AND statDate between '2016-01-11' AND '2016-01-20'
GROUP BY statDate
) AS ls ON ls.statDate = d.datefield
where d.datefield between '2016-01-11' AND '2016-01-20'