gpt4 book ai didi

MySql 触发器不会计算

转载 作者:行者123 更新时间:2023-11-29 09:13:50 27 4
gpt4 key购买 nike

我需要创建一个触发器,根据其他列值计算一个列值。数据库稍微非规范化以获得更高的性能。 (标准化不是这个问题的问题)。

问题是我想设置compute_address值的值,如果我在其中放入一个常量就可以了。但似乎这些 If 子句不起作用,我只是看不出问题所在。

下面是触发代码。非常感谢!

DELIMITER $$

USE `nth_poi_new_3`$$

DROP TRIGGER /*!50032 IF EXISTS */ `poi_address_creator`$$

CREATE
/*!50017 DEFINER = 'root'@'localhost' */
TRIGGER `poi_address_creator` BEFORE INSERT ON `poi`
FOR EACH ROW BEGIN
DECLARE full_address VARCHAR(255);
DECLARE country_string VARCHAR(100);
DECLARE region_string VARCHAR(100);
DECLARE town_string VARCHAR(100);
DECLARE address_string VARCHAR(100);

IF NEW.address <> '' THEN
SET full_address = CONCAT(NEW.address, ",");

END IF;
IF NEW.town_name IS NOT NULL THEN
SET full_address = CONCAT(full_address, NEW.town_name, ",");
ELSEIF NEW.town_id IS NOT NULL THEN
SELECT NAME INTO town_string FROM town WHERE town.town_id = NEW.town_id LIMIT 1;
SET full_address = CONCAT(full_address, town_string, ",");
END IF;

IF NEW.region_name IS NOT NULL THEN
SET full_address = CONCAT(full_address, NEW.region_name, ",");
ELSEIF NEW.region_id IS NOT NULL THEN
SELECT NAME INTO region_string FROM region WHERE region.region_id = NEW.region_id LIMIT 1;
SET full_address = CONCAT(full_address, region_string, ",");
END IF;

IF NEW.country_name IS NOT NULL THEN
SET full_address = CONCAT(full_address, NEW.country_name, ",");
ELSEIF NEW.country_id IS NOT NULL THEN
SELECT NAME INTO country_string FROM country WHERE country.country_id = NEW.country_id LIMIT 1;
SET full_address = CONCAT(full_address, country_string, ",");
END IF;

SET NEW.computed_address = full_address;

END;
$$

DELIMITER ;

最佳答案

在触发代码的底部

END; 
$$

DELIMITER ;

应该是

END$$

DELIMITER ;

关于MySql 触发器不会计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4761416/

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