MySQL query to fetch date more recent than 14 days?

MySQLMySQLi Database

Let us first create a −

mysql> create table DemoTable1392
   -> (
   -> ArrivalDate  date
   -> );
Query OK, 0 rows affected (0.43 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1392 values('2019-09-10');
Query OK, 1 row affected (0.46 sec)
mysql> insert into DemoTable1392 values('2019-09-26');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1392 values('2019-09-12');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1392 values('2018-09-20');
Query OK, 1 row affected (0.31 sec)
mysql> insert into DemoTable1392 values('2019-10-11');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select −

mysql> select * from DemoTable1392;

This will produce the following output −

+-------------+
| ArrivalDate |
+-------------+
| 2019-09-10  |
| 2019-09-26  |
| 2019-09-12  |
| 2018-09-20  |
| 2019-10-11  |
+-------------+
5 rows in set (0.00 sec)

The current date is as follows −

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2019-09-26 |
+------------+
1 row in set (0.00 sec)

Here is the query to fetch date more recent than 14 days −

mysql> select * from DemoTable1392
   -> where ArrivalDate > date_sub(curdate(), interval 14 day);

This will produce the following output −

+-------------+
| ArrivalDate |
+-------------+
| 2019-09-26  |
| 2019-10-11  |
+-------------+
2 rows in set (0.03 sec)
raja
Published on 11-Nov-2019 10:27:45
Advertisements