在 MySQL 中减去一天
若要在 MySQL 中减去一天,请使用 DATE_SUB() 方法。我们首先创建一个表 -
mysql> create table DemoTable -> ( -> AdmissionDate timestamp -> ); Query OK, 0 rows affected (1.05 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('2019-01-01'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('2018-12-31'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2017-03-13'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-01-02'); Query OK, 1 row affected (0.08 sec)
使用 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)
广告