【发布时间】:2014-02-11 18:20:10
【问题描述】:
我有一个带有 TIMESTAMP 字段“close_time”的 MySQL(5.1.67) 表“tracker”。
在同一张表中,我有字符串字段“status”。
当 status 字段更新为特定文本值(例如“Closed”)时,我想用 CURRENT_TIMESTAMP 值更新 close_time 字段。
以下是带有 INSERT 的示例: How can I write a trigger that updates rows in the same table, before the insert is commited?
我尝试过类似的方法:
CREATE TRIGGER `close_time_trigger` BEFORE UPDATE ON `tracker` FOR EACH ROW
BEGIN
IF NEW.status = 'Closed' THEN
SET NEW.close_time = CURRENT_TIMESTAMP;
END IF;
END
但我收到错误:您的 SQL 语法有错误...在第 4 行。
我尝试了一些我发现的其他变体,但它们都不起作用。 请帮助为我的案例创建正确的触发器。
【问题讨论】:
标签: mysql triggers timestamp conditional-statements