MySQL 查询来提取比 14 天更近日期的数据?
让我们首先创建一个 -
mysql> create table DemoTable1392 -> ( -> ArrivalDate date -> ); Query OK, 0 rows affected (0.43 sec)
使用 insert 在表中插入一些记录 -
mysql> insert into DemoTable1392 values('2019-09-10'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable1392 values('2019-09-26'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1392 values('2019-09-12'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1392 values('2018-09-20'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1392 values('2019-10-11'); Query OK, 1 row affected (0.11 sec)
使用 select 从表中显示所有记录 -
mysql> select * from DemoTable1392;
这将产生以下输出 -
+-------------+ | ArrivalDate | +-------------+ | 2019-09-10 | | 2019-09-26 | | 2019-09-12 | | 2018-09-20 | | 2019-10-11 | +-------------+ 5 rows in set (0.00 sec)
当前日期如下 -
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2019-09-26 | +------------+ 1 row in set (0.00 sec)
以下是提取比 14 天更近日期的查询 -
mysql> select * from DemoTable1392 -> where ArrivalDate > date_sub(curdate(), interval 14 day);
这将产生以下输出 -
+-------------+ | ArrivalDate | +-------------+ | 2019-09-26 | | 2019-10-11 | +-------------+ 2 rows in set (0.03 sec)
广告