MySQL 即使使用相同的列值也会更新 `timestamp`

MySQL `timestamp` to be updated even with same column value

我正在为我的 table 之一使用时间戳列,并使用自动更新功能。这是我的 table 架构:

mysql> desc user_rides;
+------------+--------------+------+-----+-------------------+-----------------------------+
| Field      | Type         | Null | Key | Default           | Extra                       |
+------------+--------------+------+-----+-------------------+-----------------------------+
| id         | int(11)      | NO   | PRI | NULL              | auto_increment              |
| user_id    | int(11)      | NO   | MUL | NULL              |                             
| ride_cnt   | int(11)      | YES  |     | NULL              |                             |
| created_at | timestamp    | NO   |     | CURRENT_TIMESTAMP |                             |
| updated_at | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------+--------------+------+-----+-------------------+-----------------------------+
5 rows in set (0.02 sec)

我期待的是,

  • created_at column to be initialize with the time, the row gets created and
  • updated_at column to be same as created_at and also updated when any of the columns(basically ride_cnt) get updated.

效果很好。

但我还期望即使 ride_cnt 具有相同的值,也会更新 updated_at。这样我就可以跟踪最后一次获取该行的值的时间,并且可以忽略进一步 运行.

例如:

ride_cnt = 0 的行将更新为我们 运行 更新的最新时间。这样可以在相当长的一段时间内忽略行以重新初始化。

有什么方法可以在不手动传递时间戳的情况下实现这一点?

编辑:

这里发生了什么,

mysql> insert into user_ride set user_id=7445, user_ride=0;
Query OK, 1 row affected (0.01 sec)

mysql> insert into user_ride set user_id=7009, user_ride=2;
Query OK, 1 row affected (0.00 sec)

mysql> select * from user_ride;
+----+---------+-----------+---------------------+---------------------+
| id | user_id | user_ride | created_at          | updated_at          |
+----+---------+-----------+---------------------+---------------------+
|  1 |    7445 |         0 | 2017-06-13 10:44:05 | 2017-06-13 10:44:05 |
|  2 |    7009 |         2 | 2017-06-13 10:44:18 | 2017-06-13 10:44:18 |
+----+---------+-----------+---------------------+---------------------+
2 rows in set (0.00 sec)

mysql> update user_ride set user_ride=0 where id=1;
Query OK, 0 rows affected (0.01 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> select * from user_ride;
+----+---------+-----------+---------------------+---------------------+
| id | user_id | user_ride | created_at          | updated_at          |
+----+---------+-----------+---------------------+---------------------+
|  1 |    7445 |         0 | 2017-06-13 10:44:05 | 2017-06-13 10:44:05 |
|  2 |    7009 |         2 | 2017-06-13 10:44:18 | 2017-06-13 10:44:18 |
+----+---------+-----------+---------------------+---------------------+
2 rows in set (0.00 sec)

mysql> update user_ride set user_ride=1 where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from user_ride;
+----+---------+-----------+---------------------+---------------------+
| id | user_id | user_ride | created_at          | updated_at          |
+----+---------+-----------+---------------------+---------------------+
|  1 |    7445 |         1 | 2017-06-13 10:44:05 | 2017-06-13 10:45:26 |
|  2 |    7009 |         2 | 2017-06-13 10:44:18 | 2017-06-13 10:44:18 |
+----+---------+-----------+---------------------+---------------------+
2 rows in set (0.00 sec)

我想提请注意您在上述情况下的第一次更新:

mysql> update user_ride set user_ride=0 where id=1;
Query OK, 0 rows affected (0.01 sec)
Rows matched: 1  Changed: 0  Warnings: 0

我们可以看到有一个匹配的行,但实际上没有发生更新。这样做的原因是更新不会导致数据实际发生变化。因此,updated_at 时间戳的 ON UPDATE 子句从未生效。我可以为您的问题提供两种解决方法。第一个,可能是性能最高的,就是在更新期间手动将 updated_at 列设置为当前时间戳。因此,在上面的示例中,您可以改用它:

update user_ride set user_ride = 0, updated_at = CURRENT_TIMESTAMP where id=1;

这应该会触发行的实际更新,因为自上次更新以来时间戳已更改。

另一种解决方法是找到一种方法来确保每次更新都会始终更改给定记录中的某些数据。然后,将始终应用 ON UPDATE 子句。

这似乎是一个限制,但我想 MySQL 如果基础数据本身没有更改,则不会将更改视为发生在记录上。

这个问题有点重复这个问题:

How to Force UPDATE of MySQL Record When Nothing Is Changing

但是,由于 SO 对这个问题的报道太少,我认为这个答案可能对遇到同样问题的其他人有用。

添加触发器。

CREATE TRIGGER user_ride_bu
BEFORE UPDATE ON user_ride
FOR EACH ROW
SET NEW.updated_at = NOW();

(对于那些想知道 DELIMITER 指令在哪里的人,当触发器只有一个简单的语句时不需要它们)。