gpt4 book ai didi

mysql - 如何触发一个表并更新同一个表?

转载 作者:行者123 更新时间:2023-11-29 20:32:42 29 4
gpt4 key购买 nike

我正在做一个触发器,当我更新表时,同一个表会更新一个字段。触发器是:

CREATE TRIGGER update_user
AFTER UPDATE
ON users
FOR EACH ROW
BEGIN
IF (NEW.totalPoints > 0 AND NEW.totalPoints < 200)
THEN
updateUser("rank 1", NEW.ID);
ELSEIF (NEW.totalPoints > 200 AND NEW.totalPoints < 400)
THEN
updateUser("rank 2", NEW.ID);
END IF;
END;

程序是:

CREATE PROCEDURE updateUser(newRank VARCHAR(100), IDUser INT)
BEGIN
UPDATE users SET rank = newRank WHERE ID = IDUser;
END;

我在创建过程时遇到的错误:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3

感谢您的帮助!!

最佳答案

让我们尝试使用分隔符

DELIMITER $$    
CREATE TRIGGER update_user AFTER UPDATE ON users
FOR EACH ROW
BEGIN
IF (NEW.totalPoints > 0 AND NEW.totalPoints < 200)
THEN
SET NEW.rank = 'rank 1';
ELSEIF (NEW.totalPoints > 200 AND NEW.totalPoints < 400)
THEN
SET NEW.rank = 'rank 2';
END IF;
END$$
DELIMITER ;

关于mysql - 如何触发一个表并更新同一个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38953437/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com