MySQL查询表中最近7天数据(无数据自动补0,不含本天)

2022/1/4 19:04:55

本文主要是介绍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;


这篇关于MySQL查询表中最近7天数据(无数据自动补0,不含本天)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程