如何更新表中的所有日期?
mysqlmysqli database
您可以使用 UPDATE 和 DATE_ADD() 来更新所有日期。让我们首先创建一个表 −
mysql> create table DemoTable ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientProjectDueDate date ); Query OK, 0 rows affected (1.19 sec)
使用 insert 命令在表中插入记录 −
mysql> insert into DemoTable(ClientProjectDueDate) values('2018-01-21'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(ClientProjectDueDate) values('2019-03-25'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(ClientProjectDueDate) values('2013-11-01'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(ClientProjectDueDate) values('2015-06-14'); Query OK, 1 row affected (0.23 sec)
使用 select 语句显示表中的所有记录。查询如下 −
mysql> select * from DemoTable;
这将产生以下输出 −
+----------+----------------------+ | ClientId | ClientProjectDueDate | +----------+----------------------+ | 1 | 2018-01-21 | | 2 | 2019-03-25 | | 3 | 2013-11-01 | | 4 | 2015-06-14 | +----------+----------------------+ 4 rows in set (0.00 sec)
以下是更新表中所有日期的查询 −
mysql> update DemoTable set ClientProjectDueDate=date_add(ClientProjectDueDate,INTERVAL 2 YEAR); Query OK, 4 rows affected (0.71 sec) Rows matched: 4 Changed: 4 Warnings: 0
让我们再次显示表中的所有记录 −
mysql> select * from DemoTable;
这将产生以下输出 −
+----------+----------------------+ | ClientId | ClientProjectDueDate | +----------+----------------------+ | 1 | 2020-01-21 | | 2 | 2021-03-25 | | 3 | 2015-11-01 | | 4 | 2017-06-14 | +----------+----------------------+ 4 rows in set (0.00 sec)