MySQL排除节假日,计算日期差
Posted 符华-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL排除节假日,计算日期差相关的知识,希望对你有一定的参考价值。
计算两个日期的差,并且需要排除节假日。需要两张表,一张是主表,需要计算日期差的表;一张是节假日表,存放法定节假日和周末的表。
主表
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` bigint NOT NULL AUTO_INCREMENT,
`start_time` datetime NULL DEFAULT NULL,
`end_time` datetime NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
INSERT INTO `test` VALUES (1, '2021-12-01 12:50:27', '2021-12-04 12:53:38');
INSERT INTO `test` VALUES (2, '2021-12-03 08:22:11', '2021-12-06 17:54:19');
INSERT INTO `test` VALUES (3, '2022-01-01 09:54:28', '2022-01-02 19:50:31');
INSERT INTO `test` VALUES (4, '2021-12-10 13:53:34', '2021-12-25 23:50:55');
INSERT INTO `test` VALUES (5, '2022-01-10 07:20:06', '2022-01-11 22:55:13');
INSERT INTO `test` VALUES (6, '2021-12-26 16:10:17', '2021-12-28 16:55:25');
INSERT INTO `test` VALUES (7, '2021-12-27 20:55:35', '2022-01-02 17:35:43');
INSERT INTO `test` VALUES (8, '2022-01-12 21:35:49', '2022-01-16 14:22:54');
节假日表(有21年和22年的节假日)
DROP TABLE IF EXISTS `sys_holiday`;
CREATE TABLE `sys_holiday` (
`id` bigint NOT NULL AUTO_INCREMENT,
`date` datetime NULL DEFAULT NULL COMMENT '日期',
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '节日名称',
`type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '节假日类型(0 工作日、1 周末、2 节日、3 调休)',
`week` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '1 - 7,分别表示 周一 至 周日',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '节假日表' ROW_FORMAT = DYNAMIC;
INSERT INTO `sys_holiday` VALUES (1, '2022-06-04 00:00:00', '端午节', '2', '6');
INSERT INTO `sys_holiday` VALUES (2, '2022-08-06 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (3, '2022-02-20 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (4, '2021-10-03 00:00:00', '国庆节', '2', '7');
INSERT INTO `sys_holiday` VALUES (5, '2021-09-20 00:00:00', '中秋节', '2', '1');
INSERT INTO `sys_holiday` VALUES (6, '2022-12-10 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (7, '2021-06-13 00:00:00', '端午节', '2', '7');
INSERT INTO `sys_holiday` VALUES (8, '2022-06-25 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (9, '2022-01-08 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (10, '2021-10-01 00:00:00', '国庆节', '2', '5');
INSERT INTO `sys_holiday` VALUES (11, '2022-06-18 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (12, '2022-12-04 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (13, '2022-05-28 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (14, '2021-05-16 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (15, '2021-10-02 00:00:00', '国庆节', '2', '6');
INSERT INTO `sys_holiday` VALUES (16, '2022-04-04 00:00:00', '清明节', '2', '1');
INSERT INTO `sys_holiday` VALUES (17, '2022-05-08 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (18, '2022-09-24 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (19, '2022-01-03 00:00:00', '元旦', '2', '1');
INSERT INTO `sys_holiday` VALUES (20, '2021-03-13 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (21, '2021-04-04 00:00:00', '清明节', '2', '7');
INSERT INTO `sys_holiday` VALUES (22, '2021-01-10 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (23, '2021-04-03 00:00:00', '清明节', '2', '6');
INSERT INTO `sys_holiday` VALUES (24, '2022-10-03 00:00:00', '国庆节', '2', '1');
INSERT INTO `sys_holiday` VALUES (25, '2021-01-24 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (26, '2021-02-16 00:00:00', '初五', '2', '2');
INSERT INTO `sys_holiday` VALUES (27, '2022-03-13 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (28, '2021-05-30 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (29, '2022-11-26 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (30, '2021-01-02 00:00:00', '元旦', '2', '6');
INSERT INTO `sys_holiday` VALUES (31, '2022-05-03 00:00:00', '劳动节', '2', '2');
INSERT INTO `sys_holiday` VALUES (32, '2022-05-15 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (33, '2021-02-28 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (34, '2021-08-15 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (35, '2021-04-11 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (36, '2021-07-11 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (37, '2021-01-03 00:00:00', '元旦', '2', '7');
INSERT INTO `sys_holiday` VALUES (38, '2021-08-29 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (39, '2022-05-21 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (40, '2022-11-20 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (41, '2021-03-27 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (42, '2022-01-31 00:00:00', '除夕', '2', '1');
INSERT INTO `sys_holiday` VALUES (43, '2022-07-09 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (44, '2022-02-26 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (45, '2022-11-12 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (46, '2022-06-11 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (47, '2022-12-18 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (48, '2022-09-17 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (49, '2022-08-07 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (50, '2021-08-21 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (51, '2021-06-12 00:00:00', '端午节', '2', '6');
INSERT INTO `sys_holiday` VALUES (52, '2021-01-23 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (53, '2022-04-09 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (54, '2021-11-14 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (55, '2021-09-19 00:00:00', '中秋节', '2', '7');
INSERT INTO `sys_holiday` VALUES (56, '2021-09-05 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (57, '2022-07-16 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (58, '2022-03-05 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (59, '2022-06-12 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (60, '2022-02-19 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (61, '2021-02-14 00:00:00', '初三', '2', '7');
INSERT INTO `sys_holiday` VALUES (62, '2021-01-09 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (63, '2022-10-06 00:00:00', '国庆节', '2', '4');
INSERT INTO `sys_holiday` VALUES (64, '2022-07-10 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (65, '2021-12-12 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (66, '2022-03-06 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (67, '2022-04-16 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (68, '2021-10-24 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (69, '2021-10-16 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (70, '2022-06-03 00:00:00', '端午节', '2', '5');
INSERT INTO `sys_holiday` VALUES (71, '2022-10-02 00:00:00', '国庆节', '2', '7');
INSERT INTO `sys_holiday` VALUES (72, '2021-10-06 00:00:00', '国庆节', '2', '以上是关于MySQL排除节假日,计算日期差的主要内容,如果未能解决你的问题,请参考以下文章