sql scheduler更新余额叶子

时间:2017-03-10 10:53:32

标签: mysql stored-procedures scheduler

这是我的信用假表。

creditleaveid   empid   creditleavemonth    creditedleaves
1               21      March                1

我正在使用调度程序在下个月插入此表中的信用/余额假期

DELIMITER $$
CREATE EVENT creditleavemonth
ON SCHEDULE EVERY '1' MONTH
STARTS '2017-02-01 00:00:00'
DO 
BEGIN
 CALL spAddCreditLeaves;
END$$

DELIMITER ;

存储过程spAddCreditLeaves是

DELIMITER $$

USE `attendance`$$

DROP PROCEDURE IF EXISTS `spAddCreditLeaves`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `spAddCreditLeaves`()
BEGIN
INSERT INTO creditleaves(empid,creditleavemonth)SELECT empid,MONTHNAME(CURDATE())FROM empdetails;
END$$

DELIMITER ;

现在我想通过查看每个员工的上个月记录来更新插入月份的记录。怎么做?

1 个答案:

答案 0 :(得分:0)

我得到了答案。它工作得非常棒。存储过程 -

USE `attendance`$$

DROP PROCEDURE IF EXISTS `spAddCreditLeaves`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `spAddCreditLeaves`()
BEGIN
IF MONTHNAME(CURDATE())='January'
THEN
INSERT INTO creditleaves(empid,creditleavemonth,creditmonthyear,creditedleaves) SELECT empid,MONTHNAME(CURDATE()),YEAR(CURDATE()),creditedleaves+1 FROM creditleaves WHERE empid IN (SELECT empid FROM empdetails GROUP BY empid) AND creditedleaves IN(SELECT creditedleaves FROM creditleaves WHERE creditleavemonth=MONTHNAME((CURDATE()-INTERVAL 1 MONTH)) AND creditmonthyear=YEAR((CURDATE()-INTERVAL 1 YEAR)));
ELSE
INSERT INTO creditleaves(empid,creditleavemonth,creditmonthyear,creditedleaves) SELECT empid,MONTHNAME(CURDATE()),YEAR(CURDATE()),creditedleaves+1 FROM creditleaves WHERE empid IN (SELECT empid FROM empdetails GROUP BY empid) AND creditedleaves IN(SELECT creditedleaves FROM creditleaves WHERE creditleavemonth=MONTHNAME((CURDATE()-INTERVAL 1 MONTH)) AND creditmonthyear=YEAR(CURDATE()));
END IF;
END$$

DELIMITER ;