在MySQL中选择两个日期之间的日期记录
要选择两个日期之间的日期记录,您需要使用BETWEEN关键字。让我们首先创建一个表-
mysql> create table DemoTable681(AdmissionDate datetime);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable681 values('2019-01-21'); mysql> insert into DemoTable681 values('2019-11-01'); mysql> insert into DemoTable681 values('2019-12-03'); mysql> insert into DemoTable681 values('2019-07-03'); mysql> insert into DemoTable681 values('2019-02-04'); Display all records from the table using select statement:
mysql> select *from DemoTable681;
这将产生以下输出-
+---------------------+ | AdmissionDate | +---------------------+ | 2019-01-21 00:00:00 | | 2019-11-01 00:00:00 | | 2019-12-03 00:00:00 | | 2019-07-03 00:00:00 | | 2019-02-04 00:00:00 | +---------------------+ 5 rows in set (0.00 sec)
以下是查询以选择两个日期之间的日期记录-
mysql> select *from DemoTable681 where AdmissionDate between '2019-02-01' and '2019-12-01';
这将产生以下输出-
+---------------------+ | AdmissionDate | +---------------------+ | 2019-11-01 00:00:00 | | 2019-07-03 00:00:00 | | 2019-02-04 00:00:00 | +---------------------+ 3 rows in set (0.00 sec)