首页 > 数据库技术 > 详细

mysql按日期分组统计数据

时间:2019-10-28 13:00:25      阅读:86      评论:0      收藏:0      [点我收藏+]

一,最近十二个月:没有数据的月份不展示

SELECT COUNT(*) AS visitCount, DATE_FORMAT(create_time, ‘%m‘) AS month FROM auditlog
        WHERE operate_type = 10 AND DATE_FORMAT(create_time,‘%Y-%m‘)>
        DATE_FORMAT(date_sub(curdate(), interval 12 month),‘%Y-%m‘)
        GROUP BY month;

技术分享图片

 二,最近十二个月,没有数据的补0

SELECT DATE_FORMAT(CURDATE(), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT(CURDATE(), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 1 MONTH), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 1 MONTH), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 2 MONTH), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 2 MONTH), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 3 MONTH), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 3 MONTH), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 4 MONTH), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 4 MONTH), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 5 MONTH), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 5 MONTH), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 6 MONTH), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 6 MONTH), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 7 MONTH), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 7 MONTH), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 8 MONTH), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 8 MONTH), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 9 MONTH), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 9 MONTH), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 10 MONTH), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 10 MONTH), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), ‘%Y-%m‘) AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), ‘%Y-%m‘) = DATE_FORMAT(create_time,‘%Y-%m‘)

技术分享图片

 

 

 三,所有数据按月统计,没有数据的月份不展示

select date_format(create_time, ‘%m‘) month, count(*) visitCount
from auditlog WHERE operate_type = 10
group by date_format(create_time, ‘%Y-%m‘);

技术分享图片

 

mysql按日期分组统计数据

原文:https://www.cnblogs.com/cailijuan/p/11750805.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!