我有一个简单的问题,但我还没有看到一个对我有意义的答案。我正在为学校做作业,请求是更新触发器。当在一个表中更新一行时,需要减去并添加另一个表中的计数数据。但是sql抱怨我的触发器。我需要"和"在这段代码?或者我只是错过了这一点?
DELIMITER $
CREATE TRIGGER AlterUpdate AFTER UPDATE
ON Projects_collier.Assign
FOR EACH ROW BEGIN
UPDATE Project
SET numEmployeesAssigned = numEmployeesAssigned +1
WHERE projNO = NEW.projNO
AND numEmployeesAssigned = numEmployeesAssigned -1
WHERE projNO = OLD projNO;
END$
DELIMITER ;
答案 0 :(得分:0)
WHERE
语句中不能有两个UPDATE
子句。但您可以使用UPDATE
表达式对CASE
进行修改,以获得所需的逻辑:
DELIMITER $
CREATE TRIGGER AlterUpdate AFTER UPDATE
ON Projects_collier.Assign FOR EACH ROW
BEGIN
UPDATE Project
SET
numEmployeesAssigned = CASE WHEN projNO = NEW.projNO
THEN numEmployeesAssigned + 1
WHEN projNO = OLD.projNO
THEN numEmployeesAssigned - 1 END
WHERE
projNO IN (NEW.projNO, OLD.projNO)
END$
DELIMITER ;