mysql查询最近7天的数据,没有数据自动补0
问题描述
查询数据库表中最近7天的记录
select count(*),date(create_time) as date from task where datediff(now(),create_time)<=6 group by day(create_time);
但是发现某一天没有数据,结果中没有显示当天(2017-08-28)的数据
解决思路
思路一: 可以在自己的程序中做额外的补零处理
思路二: 构建一个最近七天的结果集,然后和查询的结果集合做left join(本文采用第二种方式)
select a.click_date,b.count
from (
SELECT curdate() as click_date
union all
SELECT date_sub(curdate(), interval 1 day) as click_date
union all
SELECT date_sub(curdate(), interval 2 day) as click_date
union all
SELECT date_sub(curdate(), interval 3 day) as click_date
union all
SELECT date_sub(curdate(), interval 4 day) as click_date
union all
SELECT date_sub(curdate(), interval 5 day) as click_date
union all
SELECT date_sub(curdate(), interval 6 day) as click_date
) a left join (
select date(create_time) as datetime, count(*) as count
from arms_task
group by date(create_time)
) b on a.click_date = b.datetime;
当天2017-08-28结果显示为NULL
需要把NULL设置为0,利用ifnull函数即可
select a.click_date,ifnull(b.count,0) as count
from (
SELECT curdate() as click_date
union all
SELECT date_sub(curdate(), interval 1 day) as click_date
union all
SELECT date_sub(curdate(), interval 2 day) as click_date
union all
SELECT date_sub(curdate(), interval 3 day) as click_date
union all
SELECT date_sub(curdate(), interval 4 day) as click_date
union all
SELECT date_sub(curdate(), interval 5 day) as click_date
union all
SELECT date_sub(curdate(), interval 6 day) as click_date
) a left join (
select date(create_time) as datetime, count(*) as count
from arms_task
group by date(create_time)
) b on a.click_date = b.datetime;
mysql查询最近7天的数据,没有数据自动补0的更多相关文章
- T-SQL使用案例——结果数据前面自动补0
原文:T-SQL使用案例--结果数据前面自动补0 现象: 在开发的过程中,往往需要数字和字符串互转.在转换的过程中,可能需要把1编程00001,这样的格式.实现这种样子是有非常多的方法,本文主要提供一 ...
- MYSQL查询今天昨天本周本月等的数据
mysql查询本季度 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT *FROM表名WHERE TO_DAYS ...
- Mysql 查询一天中,每个小时数据的数量
SELECT HOUR(e.time) as Hour,count(*) as Count FROM error_log e WHERE e.date = '2017-09-02' GROUP BY ...
- Mysql 查询今天的某些时间之外的数据
SELECT * FROM `attendancealert` WHERE DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(`AlertTime`)),'%Y-%m- ...
- mysql查询sql中检索条件为大批量数据时处理
当userIdArr数组值为大批量时,应如此优化代码实现
- MySQL实现按天分组统计,提供完整日期列表,无数据自动补0
业务需求最近要在系统中加个统计功能,要求是按指定日期范围里按天分组统计数据量,并且要能够查看该时间段内每天的数据量. 解决思路直接按数据表日期字段group by统计,发现如果某天没数据,该日期是不出 ...
- Mysql 查询一天中每半小时记录的数量
SELECT HOUR(e.time)as Hour,FLOOR(MINUTE(e.time)/30) as M, COUNT(*) as Count FROM error_log e WHERE e ...
- HTML5的数据自动补齐功能
使用datalist元素,HTML5允许使用一组数据来生成自动补齐功能,现在你不需要使用第三方js代码或者类库啦! <input name="frameworks" list ...
- mysql 查询数据时按照A-Z顺序排序返回结果集
mysql 查询数据时按照A-Z顺序排序返回结果集 $sql = "SELECT * , ELT( INTERVAL( CONV( HEX( left( name, 1 ) ) , 16, ...
随机推荐
- 硬盘IOPS与读写速度
IOPS (Input/Output Per Second)即每秒的输入输出量(或读写次数),是衡量磁盘性能的主要指标之一.IOPS是指单位时间内系统能处理的I/O请求数量,一般以每秒处理的I/O请求 ...
- thymeleaf switch在表格中的使用,遇到的空行问题
switch在表格中的使用时 如果把<td>写在<div th:switch="${data.isShow}"> 里面导致外面出现很多空的<div&g ...
- 关于Android的margin(当前视图与周围视图的距离)和padding(当前视图与内部内容的距离)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- yarn学习
- 使用Vagrant创建多节点虚拟机集群
摘要: 在前一篇博客中,我介绍了使用Vagrant快速创建虚拟机,但是所创建的只是单个虚拟机.这篇博客将介绍使用Vagrant创建多节点虚拟机集群,可以作为Hadoop,Spark以及Storm等分布 ...
- ros ddns
ROS5X-6X脚本(10-15分钟执行一次) #DDNS本站帐号 :global ddnsuser "用户名" #DDNS本站密码 :global ddnspass " ...
- KVM镜像image 转换 调整
qemu-img create -f raw test.raw 8G 创建一个raw格式,大小为8G的镜像. qemu-img info disk1.qcow2 #查看镜像大小及实际占用多少空 ...
- Handlebars学习第一天
1.什么是handlebars???是用来做什么的??? 首先我们要了解几种前端页面展示的几种模式: a.Struct+vo+el表达式的模式 这种模式的特点:代码看起来比较整齐,但代码解析完才发送到 ...
- C# 文本文件的读写
// *********************************************************************** // Assembly : XXX // Auth ...
- re模块之research
2. re.research re.research扫描整个字符串并返回第一个成功的匹配. 2.1函数语法: re.search(pattern, string, flags=0) 参数 描述 pat ...