Mysql 按年、季度、月、周查询统计
User表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`username` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户名 ',
`password` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码',
`phone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '手机号',
`email` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '邮箱',
`sex` varchar(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '性别',
`createdTime` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`updatedTime` date NULL DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 26 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
插入一些数据
INSERT INTO `user` VALUES (1, 'admin', 'admin123', '139999999', '9999999@qq.com', '男', '2019-06-14 10:35:37', '2018-06-14');
INSERT INTO `user` VALUES (2, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2018-06-14');
INSERT INTO `user` VALUES (3, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2018-06-15');
INSERT INTO `user` VALUES (4, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-06-17');
INSERT INTO `user` VALUES (5, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-06-18');
INSERT INTO `user` VALUES (6, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-07-18');
INSERT INTO `user` VALUES (7, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-07-11');
INSERT INTO `user` VALUES (8, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-07-12');
INSERT INTO `user` VALUES (9, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-07-13');
INSERT INTO `user` VALUES (10, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-07-14');
INSERT INTO `user` VALUES (11, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-07-15');
INSERT INTO `user` VALUES (12, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-03-18');
INSERT INTO `user` VALUES (13, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-03-18');
INSERT INTO `user` VALUES (14, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-03-18');
INSERT INTO `user` VALUES (15, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-03-18');
INSERT INTO `user` VALUES (16, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-08-18');
INSERT INTO `user` VALUES (17, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-08-11');
INSERT INTO `user` VALUES (18, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-08-12');
INSERT INTO `user` VALUES (19, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-08-13');
INSERT INTO `user` VALUES (20, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-08-14');
INSERT INTO `user` VALUES (21, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-08-15');
INSERT INTO `user` VALUES (22, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-08-10');
INSERT INTO `user` VALUES (23, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-08-03');
INSERT INTO `user` VALUES (24, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-08-02');
INSERT INTO `user` VALUES (25, 'admin', 'admin123', '1399999999', '99999999@qq.com', NULL, '2019-06-14 10:37:52', '2019-08-01');
按日查询
SELECT DATE_FORMAT(updatedTime,'%Y-%m-%d') days,count(*) as sum,sum(id) as num,MAX(id) as max FROM user GROUP BY days
结果
按周查询
SELECT DATE_FORMAT(updatedTime,'%Y%u') weeks,count(*) as sum,sum(id) as num,MAX(id) as max FROM user where updatedTime >= '2019-08-01' and updatedTime <='2019-08-30' GROUP BY weeks
结果
按月查询
SELECT DATE_FORMAT(updatedTime,'%Y-%m') months,count(*) as sum,sum(id) as num,MAX(id) as max FROM user GROUP BY months
结果
按年查询
SELECT DATE_FORMAT(updatedTime,'%Y') years,count(*) as sum,sum(id) as num,MAX(id) as max FROM user GROUP BY years
结果
DATE_FORMAT(date,format)
根据format字符串格式化date值。下列修饰符可以被用在format字符串中:
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。