MySQL 的 DateTime Now()+5 天/小時/分鐘/秒?


若要將當前日期和時間更新為 5 天,你需要使用 Now() + 5。這將更新整個日期時間,即天、小時、分鐘和秒。為了理解這一點,我們建立一個表。建立表的查詢如下所示 −

mysql> create table UserInformationExpire
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserName varchar(10),
   -> UserInformationExpireDateTime datetime not null
   -> );
Query OK, 0 rows affected (0.83 sec)

現在,你可以使用 insert 命令在表中插入一些記錄。查詢如下所示 −

mysql> insert into UserInformationExpire(UserName,UserInformationExpireDateTime)
values('Maxwell','2019-02-09 23:56:27');
Query OK, 1 row affected (0.22 sec)
mysql> insert into UserInformationExpire(UserName,UserInformationExpireDateTime)
values('Carol','2018-11-21 15:45:21');
Query OK, 1 row affected (0.24 sec)
mysql> insert into UserInformationExpire(UserName,UserInformationExpireDateTime)
values('Bob','2019-01-22 16:30:35');
Query OK, 1 row affected (0.25 sec)
mysql> insert into UserInformationExpire(UserName,UserInformationExpireDateTime)
values('Larry','2017-12-25 17:20:33');
Query OK, 1 row affected (0.20 sec)

使用 select 語句從表中顯示所有記錄。查詢如下 −

mysql> select *from UserInformationExpire;

輸出

+----+----------+-------------------------------+
| Id | UserName | UserInformationExpireDateTime |
+----+----------+-------------------------------+
|  1 | Maxwell  | 2019-02-09 23:56:27           |
|  2 | Carol    | 2018-11-21 15:45:21           |
|  3 | Bob      | 2019-01-22 16:30:35           |
|  4 | Larry    | 2017-12-25 17:20:33           |
+----+----------+-------------------------------+
4 rows in set (0.00 sec)

以下是你將當前日期時間更新為 5 天/小時/分鐘/秒的語法時 id = 1,即當前日期是 2019-02-09,它將被更新為 2019-02-14

mysql> update UserInformationExpire set UserInformationExpireDateTime = now()+interval 5
day where Id = 1;
Query OK, 1 row affected (0.24 sec)
Rows matched: 1 Changed: 1 Warnings: 0

現在再次檢查表記錄以驗證只更新了 id 1 的日期和時間 −

mysql> select *from UserInformationExpire where Id = 1;

輸出

+----+----------+-------------------------------+
| Id | UserName | UserInformationExpireDateTime |
+----+----------+-------------------------------+
|  1 | Maxwell  | 2019-02-14 23:56:27           |
+----+----------+-------------------------------+
1 row in set (0.00 sec)

更新於: 30-7-2019

491 次瀏覽量

開啟您的 職業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.