mysql 统计数据,按照日期分组,把没有数据的日期也展示出来
因为业务需求,要统计每天的新增用户并且要用折线图的方式展示。
如果其中有一天没有新增用户的话,这一天就是空缺的,在绘制折线图的时候是不允许的,所有要求把没有数据的日期也要在图表显示。
查询2019-01-10------2019-01-20日的新增用户 ,查询出来是这样的。。。。。。完全不可以绘制图表

因为绘制图表要的是这样的数据

所以写了以下sql生成绘制图表的数据。
帮助有需要的人
---------------------------------------------------------------------------------------------------------------------------------------------------------
select sum(count) as count, regeistDates from (
select count(*) count, date_format(regeistDate,'%Y-%m-%d') regeistDates from t_account a
where a.regeistDate>='2018-12-21 00:00:00' and a.regeistDate<='2019-01-21 23:00:00'
GROUP BY regeistDates
UNION ALL
select @uu:=0 as count,regeistDates from (
select @num:=@num+1 as number,date_format(adddate('2018-12-21 00:00:00', INTERVAL @num DAY),'%Y-%m-%d') as regeistDates
from t_account a ,(select @num:=-1) t
where adddate('2018-12-21 00:00:00', INTERVAL @num DAY) < date_format('2019-01-21 23:00:00','%Y-%m-%d')
order by regeistDates ) rr
) sss GROUP BY sss.regeistDates ORDER BY regeistDates
---------------------------------------------------------------------------------------------------------------------------------------------------------
sql 的唯一的缺点是日期的排列是按照 t_account 这张表的行数计算的
注:
t_account 为你的数据中的一张表
select @num:=@num+1 as number,date_format(adddate('2018-12-21 00:00:00', INTERVAL @num DAY),'%Y-%m-%d') as regeistDates
from t_account a ,(select @num:=-1) t
以上这个sql 是重点
引申:
补充按照每周每月统计
===================================按照周统计============================================
SELECT sum(count)as count, DATE_FORMAT(a.regeistDates,'%Y-%m-%d %u') as regeistDates FROM (
SELECT * FROM (
SELECT COUNT(t.id) as count ,date_format( t.regeistDate,'%Y-%m-%d') as regeistDates FROM t_account t
where t.regeistDate>='2019-01-27 00:00:00' and t.regeistDate<='2019-03-27 00:00:00'
GROUP BY DATE_FORMAT(t.regeistDate,'%Y%u')
UNION ALL
select @uu:=0 as count,regeistDates from (
select @num:=@num+1 as number,date_format(adddate('2019-03-27 00:00:00', INTERVAL @num DAY),'%Y-%m-%d') as regeistDates
from t_order a ,(select @num:=-1) t
where adddate('2019-01-27 00:00:00', INTERVAL @num day) < date_format('2019-03-27 00:00:00','%Y-%m-%d')
order by regeistDates ) rr
) tt ORDER BY tt.regeistDates
) a GROUP BY DATE_FORMAT(a.regeistDates,'%Y%u')
=====================================按照月统计===========================================
SELECT sum(count)as count, a.regeistDates FROM (
SELECT * FROM (
SELECT COUNT(t.id) as count ,date_format( t.regeistDate,'%Y-%m') as regeistDates FROM t_account t
where t.regeistDate is not null and t.accountalias='39d0dc010ac646f88e3bf900f6fa6d33'
and t.regeistDate>='2019-01-27 00:00:00' and t.regeistDate<='2019-03-27 00:00:00'
GROUP BY DATE_FORMAT(t.regeistDate,'%Y-%m')
UNION ALL
select @uu:=0 as count,regeistDates from (
select @num:=@num+1 as number,date_format(adddate('2019-01-27 00:00:00', INTERVAL @num DAY),'%Y-%m') as regeistDates
from t_order a ,(select @num:=-1) t
where adddate('2019-01-27 00:00:00', INTERVAL @num day) < date_format('2019-03-27 00:00:00','%Y-%m-%d')
order by regeistDates ) rr
) tt ORDER BY tt.regeistDates
)a GROUP BY a.regeistDates
==================================获取日期所在周的周一日期 ===================================
SELECT t.regeistDate, DATE_ADD(date_format(t.regeistDate,'%Y-%m-%d'),INTERVAL -WEEKDAY(date_format(t.regeistDate,'%Y-%m-%d')) DAY) as regeistDates from t_account t
==========================================================================================
以上只是个人见解
如果您有好的方法可以在此文章下进行评论
-------------------------------------------------------
感谢帮忙的老王 。。。。。。哈哈哈
mysql 统计数据,按照日期分组,把没有数据的日期也展示出来的更多相关文章
- 实战:mysql统计指定架构的全部表的数据和索引大小情况-v2
PS:第一个版本号里未做输入的schema_name和table_name推断,改动了一下!再次share! #统计指定架构的全部表的数据和索引大小情况 #tablesize.sh #!/bin/sh ...
- 小程序开发笔记(八)—Js数组按日期分组显示数据
数据分组展示有两种方式,一种是后端直接传入分组格式的Json数据,另一种是我们在前端自己转换格式,这里我们在前端处理转换按日期分组的数据格式 1.例如后端返回数据格式为: [{createtime:' ...
- mysql按日期分组统计数据
最近在做一个招聘网时,需要显示一个月内企业招聘信息的发布数量,按日期分组统计,刚开始是直接从源数据库表里面进行group by,但这样子就出现日期不连续的问题了,我想要的效果是,若当天没有数据,则显示 ...
- MySQL按日期分组并统计截止当前时间的总数(实例教程)
MySQL按日期分组并统计截止当前时间的总数 建表语句 SET NAMES utf8mb4; ; -- ---------------------------- -- Table structure ...
- SQL 统计两个表的数据,按同一日期分组
思路:把两个表的数据按日期整合到临时表在按日期分组,求和. 例子: SELECT t.dateTime AS '日期',SUM(t.money) AS '表1利息',SUM(t.interest) A ...
- MySQL对数据表进行分组查询
MySQL对数据表进行分组查询(GROUP BY) GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...
- MySQL对数据表进行分组查询(GROUP BY)
MySQL对数据表进行分组查询(GROUP BY) GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...
- Mysql 根据时间戳按年月日分组统计
Mysql 根据时间戳按年月日分组统计create_time时间格式SELECT DATE_FORMAT(create_time,'%Y%u') weeks,COUNT(id) COUNT FROM ...
- mysql按某一字段分组取最大(小)值所在行的数据
mysql按某一字段分组取最大(小)值所在行的数据 mysql技巧--按某一字段分组取最大(小)值所在行的数据,这是mysql数据库程序员经常用到的在处理一些报表数据时候可以活用!那么猎微网将总结 ...
- MySQL数据中分级分组显示数据
前面已经有了SqlServer数据分级分组显示数据了.今天又来做一个MySQL数据库中的分级分组显示,SqlServer中用到了递归,这里为了简单就直接把根的数据显示为0 ,而不用递归了. 在MySQ ...
随机推荐
- 设置QtreeWidget水平滚动条
转载请注明出处:http://www.cnblogs.com/dachen408/p/7552603.html //设置treewidget水平滚动条 ui.treeWidget->header ...
- MongoDB最简单的入门教程之二 使用nodejs访问MongoDB
在前一篇教程 MongoDB最简单的入门教程之一 环境搭建 里,我们已经完成了MongoDB的环境搭建. 在localhost:27017的服务器上,在数据库admin下面创建了一个名为person的 ...
- TensorFlow低阶API(三)—— 变量
简介 TensorFlow变量是表示程序处理的共享持久状态的最佳方法. 我们使用tf.Variable类操作变量.tf.Variable表示可通过其运行操作来改变其值的张量.与tf.Tensor对象不 ...
- python基础一day4 元组
结果: join:返回一个字符串 列表转化为字符串 可迭代对象都可以 结果: 不报错什么也不执行 结果:
- Navicat连不上MySQL的解决办法
USE mysql; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '{your password}'; ...
- 使用 Pytorch 实现 skip-gram 的 word2vec
转载请注明 AIQ - 最专业的机器学习大数据社区 http://www.6aiq.com AIQ 机器学习大数据 知乎专栏 点击关注 链接地址: https://github.com/lonePa ...
- luogu P2078 朋友
题目背景 小明在A公司工作,小红在B公司工作. 题目描述 这两个公司的员工有一个特点:一个公司的员工都是同性. A公司有N名员工,其中有P对朋友关系.B公司有M名员工,其中有Q对朋友关系.朋友的朋友一 ...
- Django 多个字段关联同一外键
# -*- coding: utf-8 -*- """ Tencent is pleased to support the open source community b ...
- LeetCode(57) Insert Interval
题目 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nece ...
- PCB线宽与电流计算器--在线计算
http://eda365.com/article-12-1.html 计算线宽与载流量的关系,方便设计:单个人建议在有限的空间尽量将大电流线路加宽.