MySQL query to find expiry date (record) from the next 2 days?

MySQLMySQLi Database

For this, you can use BETWEEN keyword. Let us first create a table −

mysql> create table DemoTable
(
   ExpiryDate date
);
Query OK, 0 rows affected (0.55 sec)

Note − Let’s say the current date is 2019-08-18.

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2018-01-21');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('2019-08-20');
Query OK, 1 row affected (0.39 sec)
mysql> insert into DemoTable values('2018-08-20');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable values('2019-08-21');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

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

Here is the query to find expiry date within 2 days −

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

This will produce the following output −

+------------+
| ExpiryDate |
+------------+
| 2019-08-20 |
+------------+
1 row in set (0.00 sec)
raja
Published on 27-Sep-2019 10:29:30
Advertisements