使用BETWEEN和INTERVAL实现此目的。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> AdmissionDate date -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('2019-09-30'); mysql> insert into DemoTable values('2019-10-01'); mysql> insert into DemoTable values('2019-03-30'); mysql> insert into DemoTable values('2019-04-24');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+---------------+ | AdmissionDate | +---------------+ | 2019-09-30 | | 2019-10-01 | | 2019-03-30 | | 2019-04-24 | +---------------+ 4 rows in set (0.00 sec)
以下是在MySQL中从当前日期选择当前日期和3个月之间的日期的查询-
mysql> select *from DemoTable WHERE AdmissionDate BETWEEN curdate() - INTERVAL 3 MONTH AND curdate();
输出结果
+---------------+ | AdmissionDate | +---------------+ | 2019-03-30 | | 2019-04-24 | +---------------+ 2 rows in set (0.00 sec)