我有桌子up_files和fgusers3。 触发器应该在插入up_files之后更新fgusers3。
up_file:id,uname,days,d_leave
fgusers3:id,名称,AnualLeave,cf
我尝试了if if else语句,并且工作正常。每次用户插入数据时,触发器都会更新fgusers3表中的AnualLeave行和cf行。
DROP TRIGGER IF EXISTS `after_insert`;
CREATE DEFINER=`root`@`localhost`
TRIGGER `after_EL` BEFORE INSERT ON `up_files`
FOR EACH ROW
UPDATE fgusers3
SET fgusers3.AnualLeave = fgusers3.AnualLeave - NEW.days + fgusers3.cf,fgusers3.cf = 0
WHERE fgusers3.name = NEW.uname AND NEW.d_leave = 'Emergency Leave'
在添加if else语句之后,出现错误。请帮助我是mysql触发器的新手:
DELIMITER #
CREATE TRIGGER `after_insert` BEFORE INSERT ON `up_files` FOR EACH ROW
BEGIN
UPDATE fgusers3
IF(fgusers3.cf < NEW.days ) THEN
SET fgusers3.AnualLeave = fgusers3.AnualLeave - NEW.days +fgusers3.cf,fgusers3.cf = 0
WHERE fgusers3.name = NEW.uname AND NEW.d_leave = 'Emergency Leave' ;
ELSE
SET fgusers3.cf = fgusers3.cf - NEW.days
WHERE fgusers3.name = NEW.uname AND NEW.d_leave = 'Emergency Leave' ;
END IF ;
END#
DELIMITER ;
说明
我尝试使用户插入“紧急假期”,如果有的话,天数将扣除年假或从去年结转(cf)。
如果可用cf超过一天,那么它将从cf中扣除,
否则将从年假中扣除
答案 0 :(得分:0)
我认为这更像您想要的。
DELIMITER $$
CREATE TRIGGER test_trig BEFORE INSERT ON up_files
FOR EACH ROW
bEGIN
IF(fgusers3.cf < NEW.days ) THEN
UPDATE fgusers3
SET fgusers3.AnualLeave = fgusers3.AnualLeave - NEW.days +fgusers3.cf,fgusers3.cf = 0
WHERE fgusers3.name = NEW.uname AND NEW.d_leave = 'Emergency Leave' ;
ELSE
UPDATE fgusers3
SET fgusers3.cf = fgusers3.cf - NEW.days
WHERE fgusers3.name = NEW.uname AND NEW.d_leave = 'Emergency Leave' ;
END if;
end $$
delimiter ;
如果否,则将数据采样为问题中的文本将有所帮助。