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,不含本天)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-20部署MySQL集群入门:新手必读指南
- 2024-11-20部署MySQL集群教程:初学者指南
- 2024-11-20部署MySQL集群项目实战:新手教程
- 2024-11-20部署MySQL集群资料:新手入门教程
- 2024-11-20MySQL集群部署教程:入门级详解
- 2024-11-20MySQL集群教程:入门与实践指南
- 2024-11-20部署MySQL集群教程:新手入门指南
- 2024-11-20MySQL读写分离教程:轻松入门
- 2024-11-20部署MySQL集群入门:一步一步搭建你的数据库集群
- 2024-11-19部署MySQL集群学习:入门教程