mysql 5.6.10:before update触发器只对常量有效?

ubbxdtey  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(424)

我试图获取一个触发器,用当前时间戳更新一行的值,并且该触发器插入的表中正在使用相同的时间戳。触发器与set new.last\u update='some constant'一起工作,但添加变量或select all时,行中的值为null。

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 ;
liwlm1x9

liwlm1x91#

不要这样做: 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` ,你认为它“不起作用”。

相关问题