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, ...
随机推荐
- 请描述一下 cookies,sessionStorage和localStorage的区别?
cookie在浏览器和服务器间来回传递. sessionStorage和localStorage不会sessionStorage和localStorage的存储空间更大:sessionStorage和 ...
- js中的web加密
js中的web加密 window.crypto.subtle只会在安全模式下有用,也就是https环境下 创建摘要(硬解) var i = new TextEncoder('utf-8').encod ...
- 核主成分分析(Kernel Principal Component Analysis, KPCA)的公式推导过程
KPCA,中文名称”核主成分分析“,是对PCA算法的非线性扩展,言外之意,PCA是线性的,其对于非线性数据往往显得无能为力,例如,不同人之间的人脸图像,肯定存在非线性关系,自己做的基于ORL数据集的实 ...
- Python基础部分
Python基础一 Python基础二 字符串相关操作一 列表及元组 字典介绍及其操作 Python 中的 is 和 == 编码和解码 Python中的 set 与 深浅拷贝 Python 的文件操作
- bundle install 老是中断,可以在gemfile里面把source换成taobao源,可以自动安装
bundle install 老是中断,可以在gemfile里面把source换成taobao源,可以自动安装
- 基于 DirectX11 的 MMDViewer 04-渲染目标视图和多视口
上篇文章给出了一个简单并且可以运行的渲染框架,接下来将介绍框架中的渲染管线构成. 1.创建渲染管线 在你创建完一个窗口后,接着便要创建渲染管线,使用的函数是 D3D11CreateDeviceAndS ...
- 从一个子视图或者一个View中刷新其他UITableView
被问到了一个问题:如何从一个子视图或者一个View中刷新其他UITableView,常规的写法可能是这样的 TestTVC*testTVC =[[TestTVC alloc] init];[testT ...
- iPhone开发随想:rand()还是arc4random()
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://bj007.blog.51cto.com/1701577/544006 今天在iP ...
- drbd mysql
使用drbd前 升级内核 yum install kernel* 重启 4.1.准备: 两个节点ha-node1和ha-node2均按照centos7.0系统,每个节点两块磁盘,一块用作根分区一块用作 ...
- Think In Java 读后感
近期拜读了Think in Java 一书,这里是一些读后感. 此书不仅仅是市面上那种教会你怎么用系统API来编程的书,那种书太多. 此书不仅仅从头开始讲述了如何 ...