gpt4 book ai didi

mysql - 在 sql 触发器中使用 else if 时出错

转载 作者:可可西里 更新时间:2023-11-01 06:31:20 24 4
gpt4 key购买 nike

我不确定我的代码有什么问题。

 delimiter $$
CREATE TRIGGER updateRestaurantAtributes
AFTER UPDATE ON fields_data
FOR EACH ROW BEGIN
IF (NEW.fieldid = 1) THEN
UPDATE restaurants
SET address1 = NEW.data_txt
Where rid = NEW.itemid;
ELSE IF (NEW.fieldid = 2) THEN
UPDATE restaurants
SET address2 = NEW.data_txt
Where rid = NEW.itemid;
END IF;
END$$

以上版本无效。它说“END”(最后一行)附近有语法错误。但是当我使用时它会起作用

delimiter $$
CREATE TRIGGER updateRestaurantAtributes
AFTER UPDATE ON fields_data
FOR EACH ROW BEGIN
IF (NEW.fieldid = 1) THEN
UPDATE restaurants
SET address1 = NEW.data_txt
Where rid = NEW.itemid;
END IF;
IF (NEW.fieldid = 2) THEN
UPDATE restaurants
SET address2 = NEW.data_txt
Where rid = NEW.itemid;
END IF;
END$$

我不知道为什么。我错过了什么吗?

最佳答案

代替 ELSE IFMySQL's syntax使用 ELSEIF(没有空格)。

 delimiter $$
CREATE TRIGGER updateRestaurantAtributes
AFTER UPDATE ON fields_data
FOR EACH ROW BEGIN
IF (NEW.fieldid = 1) THEN
UPDATE restaurants
SET address1 = NEW.data_txt
Where rid = NEW.itemid;
ELSEIF (NEW.fieldid = 2) THEN
UPDATE restaurants
SET address2 = NEW.data_txt
Where rid = NEW.itemid;
END IF;
END$$

尽管您可以通过添加额外的 END IF 使其与 ELSE IF 中的空格一起使用。通过使用空格,您可以有效地启动第二个 IF 语句,该语句必须独立于第一个外部 IF 语句关闭。

/* Might work */
delimiter $$
CREATE TRIGGER updateRestaurantAtributes
AFTER UPDATE ON fields_data
FOR EACH ROW BEGIN
IF (NEW.fieldid = 1) THEN
UPDATE restaurants
SET address1 = NEW.data_txt
Where rid = NEW.itemid;
/* Opens a seconds IF block which must be closed */
ELSE IF (NEW.fieldid = 2) THEN
UPDATE restaurants
SET address2 = NEW.data_txt
Where rid = NEW.itemid;
/* Close inner IF block */
END IF;
END IF;
END$$

关于mysql - 在 sql 触发器中使用 else if 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8685957/

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