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 是重点

以上只是个人见解

如果您有好的方法可以在此文章下进行评论

-------------------------------------------------------

感谢帮忙的老王 。。。。。。哈哈哈

猜你喜欢

转载自www.cnblogs.com/zhengguangpan/p/10308886.html
今日推荐