sql触发器从另一个字段设置字段值

时间:2018-06-30 20:07:27

标签: mysql sql

我试图将触发器设置为sql,以使用表c_p_r内的“ title”字段中的值更新gf的值,但是语法上出现错误。有人可以指出我错了吗?

  CREATE TRIGGER setgf
    AFTER UPDATE ON c_p_r
    FOR EACH ROW BEGIN
      TRUNCATE gf
      UPDATE gf
      SET gf = title
      FROM c_p_r

这件事回答了我

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UPDATE gf
  SET gf = title
  FROM c_p_r' at line 5

有人可以指出我的错吗?

1 个答案:

答案 0 :(得分:0)

您的UPDATE sql查询格式错误。另外,如果您尝试在更新过程中将一个字段值复制到另一字段。您应该使用BEFORE UPDATE处理程序。我会这样写查询:

 CREATE TRIGGER setgf
    BEFORE UPDATE ON c_p_r
    FOR EACH ROW BEGIN
      SET NEW.gf = COALESCE(NEW.gf, NEW.title); //this will take care of not overriding the gf if it was part of update statement
    END