我正在尝试获取一个触发器,以使用当前时间戳更新行的值,并且该触发器正在插入的表中正在使用相同的时间戳。触发器使用set new.last_update ='some constant'设置,但添加变量或全选时,该行中的值为空值。
drop trigger if exists event_update_history;
delimiter //
create trigger event_update_history before update on Events
for each row
begin
declare history_timestamp timestamp;
set @history_timestamp = timestamp(unix_timestamp());
if (new.current_state <> old.current_state) then
insert into Event_History (evt_id, evt_state, time_of_change, userid) values (new.evt_id, new.current_state, history_timestamp, new.last_updated_by);
end if;
set new.last_update = history_timestamp; ## THIS IS THE PART THAT IS NOT WORKING but works if you send a constant
end; //
delimiter ;
答案 0 :(得分:0)
不要做:timestamp(unix_timestamp())
mysql> select timestamp(unix_timestamp()), unix_timestamp();
+-----------------------------+------------------+
| timestamp(unix_timestamp()) | unix_timestamp() |
+-----------------------------+------------------+
| NULL | 1542320614 |
+-----------------------------+------------------+
1 row in set, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+----------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------+
| Warning | 1292 | Incorrect datetime value: '1542320614' |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)
也就是说,由于您收到NULL
,因此您认为它“不起作用”。