gpt4 book ai didi

MySQL 触发 IF 条件

转载 作者:行者123 更新时间:2023-11-29 21:06:41 30 4
gpt4 key购买 nike

我想在插入另一个表时填充表中的一些列值,并为特定列设置条件。我使用触发器:

CREATE TRIGGER inserttrigger AFTER INSERT ON table1 FOR EACH ROW INSERT INTO table2 SET col1 = NEW.col1, col2 = NEW.col2;  IF LENGTH(NEW.col3) > 20 THEN SET col3 = NEW.col3 END IF;

我在 IF 条件附近遇到语法错误:

ERROR 1064 (42000): 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 'END IF' at line 1

最佳答案

delimiter |
CREATE TRIGGER inserttrigger AFTER INSERT ON table1
FOR EACH ROW
BEGIN
INSERT INTO table2
SET col1 = NEW.col1,
col2 = NEW.col2,
col3 = case when LENGTH(NEW.col3) > 20
then NEW.col3
end;
END
|
delimiter ;

关于MySQL 触发 IF 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36754535/

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