正如标题所说,我有一个表,其中有一列名为 last_update,类型为 DATE。我想制作一个 trigger ,每次我更新一行时将其设置为 CURDATE() (为此特定行设置)。我试过这个:
delimiter $$
CREATE TRIGGER before_customer_update before update on customer for each row
begin
update customer set last_update = CURDATE() where id = old.id;
end $$
delimiter ;
但是得到一个错误
can't update table in stored function/trigger because it is already used by statement
我该如何解决?
最佳答案
将列定义更改为
会更容易last_update timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
如果你真的想在触发器中执行(用NEW
引用当前记录):
替换
update customer set last_update = CURDATE() where id = old.id;
与
set NEW.last_update = CURDATE();
关于MySQL 更新后自动更新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43425755/