经工作需要,要用到根据一个时间要在之后3个工作日修改状态,想了好久,终于想出了一个方法,感觉有点low
1.首先创建一张节假日表
CREATE TABLE `bs_holidays` (
`h_id` date NOT NULL COMMENT '法定節假日日期',
`the_year` varchar(10) COLLATE utf8_bin DEFAULT NULL COMMENT '年份',
`days_ide` bigint(20) DEFAULT NULL COMMENT '節假日標識ID',
`daysname` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '節假日標識名稱',
`crter` bigint(20) DEFAULT NULL COMMENT '創建人',
`crt_date` datetime DEFAULT NULL COMMENT '創建日期',
`crt_ip` varchar(15) COLLATE utf8_bin DEFAULT NULL COMMENT '創建人ip',
`opper` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '修改人',
`opp_date` date DEFAULT NULL COMMENT '修改日期',
`opp_ip` varchar(16) COLLATE utf8_bin DEFAULT NULL COMMENT '修改人ip',
PRIMARY KEY (`h_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='國家法定節假日記錄表';
2.通过面向过程(函数)写筛选工作日:
参数:_end varchar(20),_delay int
返回类型:varchar(20)
BEGIN
#Routine body goes here..
#DECLARE _end datetime;
DECLARE _reweek INT;
DECLARE intNums INT;
DECLARE i INT;
SET i = 1;
WHILE i<=_delay DO
SELECT DAYOFWEEK(STR_TO_DATE(_end,'%Y-%m-%d')) INTO _reweek;
IF _reweek = 1 THEN
SET _end = DATE_ADD(_end,INTERVAL 1 DAY);
ELSEIF _reweek = 7 THEN
SET _end = DATE_ADD(_end,INTERVAL 1 DAY);
ELSE
SELECT COUNT(t.h_id) INTO intNums FROM bs_holidays t WHERE t.h_id = _end;
IF intNums = 0 THEN
SET _end = DATE_ADD(_end,INTERVAL 1 DAY);
SET i = i+1;
ELSE
SET _end = DATE_ADD(_end,INTERVAL 1 DAY);
END IF;
END IF;
END WHILE;
RETURN _end;
END