要在MySQL中减去一天,请使用DATE_SUB()方法。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> AdmissionDate timestamp -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('2019-01-01'); mysql> insert into DemoTable values('2018-12-31'); mysql> insert into DemoTable values('2017-03-13'); mysql> insert into DemoTable values('2019-01-02');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+---------------------+ | AdmissionDate | +---------------------+ | 2019-01-01 00:00:00 | | 2018-12-31 00:00:00 | | 2017-03-13 00:00:00 | | 2019-01-02 00:00:00 | +---------------------+ 4 rows in set (0.00 sec)
这是减去一天的查询-
mysql> select date_sub(AdmissionDate,interval 1 day) from DemoTable;
这将产生以下输出-
+----------------------------------------+ | date_sub(AdmissionDate,interval 1 day) | +----------------------------------------+ | 2018-12-31 00:00:00 | | 2018-12-30 00:00:00 | | 2017-03-12 00:00:00 | | 2019-01-01 00:00:00 | +----------------------------------------+ 4 rows in set (0.00 sec)