Selecting records 15 days before today in MySQL?


For this, you can use the concept of INTERVAL and DATE_SUB(). Let us first create a table −

mysql> create table DemoTable1845
     (
     ArrivalDate date
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1845 values('2019-12-02');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1845 values('2019-11-18');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1845 values('2019-12-18');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1845 values('2019-12-25');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1845 values('2019-11-15');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1845;

This will produce the following output −

+-------------+
| ArrivalDate |
+-------------+
| 2019-12-02  |
| 2019-11-18  |
| 2019-12-18  |
| 2019-12-25  |
| 2019-11-15  |
+-------------+
5 rows in set (0.00 sec)

The current date is as follows −

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2019-12-02 20:47:05 |
+---------------------+
1 row in set (0.00 sec)

Here is the query to fetch records 15 days before today −

mysql> select * from DemoTable1845
     where ArrivalDate < DATE_SUB(NOW(), INTERVAL 15 DAY);

This will produce the following output −

+-------------+
| ArrivalDate |
+-------------+
| 2019-11-15  |
+-------------+
1 row in set (0.00 sec)

Updated on: 26-Dec-2019

768 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements