Mysql 按年、季度、月、周查询统计

时间:2023-01-03 15:08:04

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%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

结果
Mysql 按年、季度、月、周查询统计