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;