MySQL查询以查找未来2天的有效期(记录)?

为此,您可以使用BETWEEN关键字。让我们首先创建一个表-

mysql> create table DemoTable
(
   ExpiryDate date
);

注意-假设当前日期为2019-08-18。

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable values('2018-01-21');
mysql> insert into DemoTable values('2019-08-20');
mysql> insert into DemoTable values('2018-08-20');
mysql> insert into DemoTable values('2019-08-21');

使用select语句显示表中的所有记录-

mysql> select *from DemoTable;

这将产生以下输出-

+------------+
| ExpiryDate |
+------------+
| 2018-01-21 |
| 2019-08-20 |
| 2018-08-20 |
| 2019-08-21 |
+------------+
4 rows in set (0.00 sec)

这是查询以查找2天内的到期日期-

mysql> select *from DemoTable where ExpiryDate between curdate() AND curdate() + 2;

这将产生以下输出-

+------------+
| ExpiryDate |
+------------+
| 2019-08-20 |
+------------+
1 row in set (0.00 sec)