您可以将UPDATE与DATE_ADD()一起使用来更新所有日期。让我们首先创建一个表-
mysql> create table DemoTable ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientProjectDueDate date );
使用插入命令在表中插入记录-
mysql> insert into DemoTable(ClientProjectDueDate) values('2018-01-21'); mysql> insert into DemoTable(ClientProjectDueDate) values('2019-03-25'); mysql> insert into DemoTable(ClientProjectDueDate) values('2013-11-01'); mysql> insert into DemoTable(ClientProjectDueDate) values('2015-06-14');
使用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); 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)