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)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP