MySQL DateTime Now()+5 天/小时/分钟/秒?
mysqlmysqli database
要将当前日期和时间更新为 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)
以下是将 id = 1 的当前日期时间更新为 5 天/小时/分钟/秒的语法,即当前日期为 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: 00
现在再次检查表记录以验证仅 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)