Select rows from a table with date between 90 days ago and now in MySQL


Use MySQL INTERVAL for this. Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-07-10');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('2019-09-02');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('2019-06-01');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable values('2019-05-10');
Query OK, 1 row affected (0.13 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 −

+------------+
| DueDate    |
+------------+
| 2019-07-10 |
| 2019-09-02 |
| 2019-06-01 |
| 2019-05-10 |
| 2019-08-21 |
+------------+
5 rows in set (0.00 sec)

Following is the query to select rows from a table with a date between - 90days ago and now −

mysql> select *from DemoTable
where DueDate between now() - interval 90 day and now();

This will produce the following output −

+------------+
| DueDate    |
+------------+
| 2019-07-10 |
| 2019-08-21 |
+------------+
2 rows in set (0.00 sec)

Updated on: 03-Oct-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements