如何更新 MySQL 表的時間戳欄位?


首先讓我們建立一個表 -

mysql> create table DemoTable
   -> (
   -> PunchOut timestamp,
   -> PunchStatus tinyint(1)
   -> );
Query OK, 0 rows affected (0.51 sec)

使用插入命令在表中插入一些記錄 -

mysql> insert into DemoTable values('2019-01-31 6:30:10',1);
Query OK, 1 row affected (0.22 sec)

mysql> insert into DemoTable values('2019-02-06 4:10:13',0);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values('2018-12-16 03:00:30',0);
Query OK, 1 row affected (0.16 sec)

mysql> insert into DemoTable values('2016-11-25 02:10:00',1);
Query OK, 1 row affected (0.22 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

輸出

+---------------------+-------------+
| PunchOut            | PunchStatus |
+---------------------+-------------+
| 2019-01-31 06:30:10 |           1 |
| 2019-02-06 04:10:13 |           0 |
| 2018-12-16 03:00:30 |           0 |
| 2016-11-25 02:10:00 |           1 |
+---------------------+-------------+
4 rows in set (0.00 sec)

以下是在 MySQL 表中更新時間戳欄位的查詢。我們將當前日期設定到 PunchStatus 為 0 的欄位 -

注意 - 當前時間和日期是 2019-06-30 13:43:45

mysql> update DemoTable set PunchOut=now() where PunchStatus=0;
Query OK, 2 rows affected (0.19 sec)
Rows matched: 2  Changed: 2 Warnings: 0

讓我們再次檢查表記錄 -

mysql> select *from DemoTable;

輸出

+---------------------+-------------+
| PunchOut            | PunchStatus |
+---------------------+-------------+
| 2019-01-31 06:30:10 |           1 |
| 2019-06-30 13:43:45 |           0 |
| 2019-06-30 13:43:45 |           0 |
| 2016-11-25 02:10:00 |           1 |
+---------------------+-------------+
4 rows in set (0.00 sec)

更新於: 30-Jul-2019

2K+ 次瀏覽

開啟你的 職業

完成課程並獲得認證

開始吧
廣告
© . All rights reserved.