MySql 触发器很有趣,但也很棘手。
我有一些问题,我想运行一次触发器after insert on
。我想在插入行后运行一次触发器,每个表都有类似的东西吗????
如何使此触发器仅运行一次,而不是为创建的每一行运行。
CREATE
DEFINER=`root`@`localhost`
TRIGGER `mydb`.`leave_taken_trigger`
AFTER INSERT ON `mydb`.`leave`
FOR EACH ROW
BEGIN
set @lt:= (SELECT count(*) FROM `leave` where (staff_leave_application_staff_id_staff = new.staff_leave_application_staff_id_staff and leave_type_id_leave_type = new.leave_type_id_leave_type) and active = 1 );
INSERT INTO `leave_taken`(leave_type_id_leave_type, staff_id_staff, taken_days, updated)
VALUES (new.leave_type_id_leave_type, new.staff_leave_application_staff_id_staff, IFNULL(@lt,0), CURDATE())
ON DUPLICATE KEY UPDATE taken_days=IFNULL(@lt,0), updated = CURDATE();
END$$