Mysql学习总结(68)——MYSQL统计每天、每周、每月、每年数据 SQL 总结

-- 查询每天的数据             
 SELECT COUNT(1) AS total, DATE_FORMAT(created_time,'%Y-%m-%d') AS days FROM task_execution GROUP BY days desc;
 
 -- 查询每周的数据
SELECT COUNT(1) AS total, WEEK(created_time) AS weeks FROM task_execution GROUP BY weeks desc;

-- 查询每月的数据
SELECT COUNT(1) AS total, MONTH(created_time) AS months FROM task_execution GROUP BY months desc;

-- 查询每年的数据
SELECT COUNT(1) AS total, YEAR(created_time) AS years FROM task_execution GROUP BY years desc;

注意:
-- DATE_ADD(createTime,INTERVAL 1 DAY)           得到指定日期后一天的日期/把1改为任意数字就可以得到后N天的日期
-- DATE_SUB(createTime,INTERVAL 1 DAY)           得到指定日期前一天的日期/把1改为任意数字就可以得到前N天的日期

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览

应支付9.90元
点击重新获取
扫码支付

支付成功即可阅读