如何通过单个MySQL查询向日期添加一年零两天?
为此,请在MySQL中使用INTERVAL。让我们首先创建一个表-
mysql> create table DemoTable1376 -> ( -> AdmissionDate date -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1376 values('2018-01-21'); mysql> insert into DemoTable1376 values('2017-12-01'); mysql> insert into DemoTable1376 values('2018-11-02'); mysql> insert into DemoTable1376 values('2019-03-14');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1376;
这将产生以下输出-
+---------------+ | AdmissionDate | +---------------+ | 2018-01-21 | | 2017-12-01 | | 2018-11-02 | | 2019-03-14 | +---------------+ 4 rows in set (0.00 sec)
以下是在MySQL中向日期添加一年零两天的查询-
mysql> select date_add(date_add(AdmissionDate,interval 1 year),interval 2 day) AS AfterAddingOneYearTwoDays from DemoTable1376;
这将产生以下输出-
+---------------------------+ | AfterAddingOneYearTwoDays | +---------------------------+ | 2019-01-23 | | 2018-12-03 | | 2019-11-04 | | 2020-03-16 | +---------------------------+ 4 rows in set (0.00 sec)