MySQL时间段查询从当前日期起14周的间隔中获取日期记录?
为此,您可以使用BETWEEN关键字。让我们首先创建一个表-
mysql> create table DemoTable ( ArrivalDate date );
假设当前日期为2019-08-31。
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('2019-04-21'); mysql> insert into DemoTable values('2019-03-01'); mysql> insert into DemoTable values('2019-09-01'); mysql> insert into DemoTable values('2019-08-31'); mysql> insert into DemoTable values('2019-06-30'); mysql> insert into DemoTable values('2019-06-01');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------------+ | ArrivalDate | +-------------+ | 2019-04-21 | | 2019-03-01 | | 2019-09-01 | | 2019-08-31 | | 2019-06-30 | | 2019-06-01 | +-------------+ 6 rows in set (0.00 sec)
以下是对时间段查询的查询-
mysql> select *from DemoTable where ArrivalDate between date_sub(curdate(),interval 14 week) and curdate();
这将产生以下输出-
+-------------+ | ArrivalDate | +-------------+ | 2019-08-31 | | 2019-06-30 | | 2019-06-01 | +-------------+ 3 rows in set (0.00 sec)