MySQL查询表中最近7天数据(无数据自动补0,不含本天)
作者:互联网
MySQL查询表中最近7天数据(无数据自动补0,不含本天)
select a.date,
ifnull(b.total_count, 0) as total_count,
from (
SELECT date_sub(curdate(), interval 1 day) as date
union all
SELECT date_sub(curdate(), interval 2 day) as date
union all
SELECT date_sub(curdate(), interval 3 day) as date
union all
SELECT date_sub(curdate(), interval 4 day) as date
union all
SELECT date_sub(curdate(), interval 5 day) as date
union all
SELECT date_sub(curdate(), interval 6 day) as date
union all
SELECT date_sub(curdate(), interval 7 day) as date
) a
left join (
select date(create_time) as date,
count(*) as total_count,
from report
where DATE_SUB(curdate(), INTERVAL 7 DAY) <= date(create_time) and date(create_time) < curdate()
group by date(create_time)
) b on a.date = b.date order by a.date;
标签:interval,sub,MySQL,本天,curdate,表中,date,day,SELECT 来源: https://www.cnblogs.com/huturen/p/15762620.html