问题描述

查询数据库表中最近7天的记录

select count(*),date(create_time) as date from task where datediff(now(),create_time)<=6  group by day(create_time); 

但是发现某一天没有数据,结果中没有显示当天(2017-08-28)的数据

解决思路

  1. 思路一: 可以在自己的程序中做额外的补零处理

  2. 思路二: 构建一个最近七天的结果集,然后和查询的结果集合做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的更多相关文章

  1. T-SQL使用案例——结果数据前面自动补0

    原文:T-SQL使用案例--结果数据前面自动补0 现象: 在开发的过程中,往往需要数字和字符串互转.在转换的过程中,可能需要把1编程00001,这样的格式.实现这种样子是有非常多的方法,本文主要提供一 ...

  2. MYSQL查询今天昨天本周本月等的数据

    mysql查询本季度 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT *FROM表名WHERE TO_DAYS ...

  3. Mysql 查询一天中,每个小时数据的数量

    SELECT HOUR(e.time) as Hour,count(*) as Count FROM error_log e WHERE e.date = '2017-09-02' GROUP BY ...

  4. Mysql 查询今天的某些时间之外的数据

    SELECT * FROM `attendancealert` WHERE DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(`AlertTime`)),'%Y-%m- ...

  5. mysql查询sql中检索条件为大批量数据时处理

    当userIdArr数组值为大批量时,应如此优化代码实现

  6. MySQL实现按天分组统计,提供完整日期列表,无数据自动补0

    业务需求最近要在系统中加个统计功能,要求是按指定日期范围里按天分组统计数据量,并且要能够查看该时间段内每天的数据量. 解决思路直接按数据表日期字段group by统计,发现如果某天没数据,该日期是不出 ...

  7. Mysql 查询一天中每半小时记录的数量

    SELECT HOUR(e.time)as Hour,FLOOR(MINUTE(e.time)/30) as M, COUNT(*) as Count FROM error_log e WHERE e ...

  8. HTML5的数据自动补齐功能

    使用datalist元素,HTML5允许使用一组数据来生成自动补齐功能,现在你不需要使用第三方js代码或者类库啦! <input name="frameworks" list ...

  9. mysql 查询数据时按照A-Z顺序排序返回结果集

    mysql 查询数据时按照A-Z顺序排序返回结果集 $sql = "SELECT * , ELT( INTERVAL( CONV( HEX( left( name, 1 ) ) , 16, ...

随机推荐

  1. Linux 之 inotify+rsync 备份文件系统

    一.需求 1.线上有不同的机房,并且每个机房所对公网开放端口不一样. 2.A机房中的a机器是台文件服务器,需要备份到B机房中的b机器,以及C机房中的c机器. 3.并且保持实时同步.只要a上面的文件有改 ...

  2. SharePoint 事件 7363:对象缓存:缓存使用的超级读者帐户没有足够的权限访问SharePoint数据库。

    转自MSND:http://technet.microsoft.com/zh-cn/library/ff758656(v=office.14) 对象缓存存储 Microsoft SharePoint ...

  3. Julia - 函数运算符

    Julia 中,大多数运算符都是支持特定语法的函数 && . || 等短路运算是例外,它们不是函数,因为短路求值先算前面的值,再算后面的值 对于函数运算符,可以像其它函数一样,把参数列 ...

  4. wkhtmktopdf

    1: sudo apt-get install wkhtmltopdf   2: sudo apt-get xvfb   3: 切换到root用户: echo 'xvfb-run --server-a ...

  5. PHP RSA加密解密

    1.生成密钥和公钥 开始前需要准备openssl环境 linux 需要安装openssl工具包,传送门http://www.openssl.org/source/ window 下需要安装openss ...

  6. Android真机调试手动添加程序包的LogCat

    android真机调试有时候看LogCat 时,有时候那个跑的本程序的LogCat 没有出现而是 出现的是" All messages (no filters) " .此时 的Lo ...

  7. C++虚函数表理解

    一,思维模式图 二,代码验证 class A { public: A(int x) { fProtected = x; } float GetFProtected() { return fProtec ...

  8. <转>Linux环境进程间通信(五): 共享内存(上)

    http://www.ibm.com/developerworks/cn/linux/l-ipc/part5/index1.html 采用共享内存通信的一个显而易见的好处是效率高,因为进程可以直接读写 ...

  9. iOS开发基础控件--UIButton

    01 //这里创建一个圆角矩形的按钮 02     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 03 ...

  10. 如何建立ElasticSearch里的mappings?

    刚接触elasticsearch,好多东西都不会用,百度了很多,都看不懂,终于摸索出了最简单的通过http建立mappings的方法~ 有人在建立mappings报各种错误,首先,如果你的这个索引中已 ...