我正在使用時間戳列到我的表格之一,並使用自動更新功能。這裏是我的表模式:即使使用相同的列值,MySQL'timestamp'也要更新
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
柱要與時間初始化,該行被創建和updated_at
列是與created_at
相同,並且在更新任何列(基本上ride_cnt
)時也會更新。
This works great。
但我也期待的是updated_at
即使ride_cnt
具有相同的值更新。這樣我就可以跟蹤上次訪問該行的值的時間,並可以將其忽略以供進一步運行。
例如:
與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)
您是否嘗試過觸發器? – Namphibian
如果使用記錄中已有的相同值進行更新,則更新仍會發生,並且當前時間戳仍應更新。我在這裏錯過了什麼嗎? –
@TimBiegeleisen,即使我假設是一樣的,但事實並非如此。新增示例。 – avisheks