MySQL time period query to fetch date records from interval of 14 weeks from current date?


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

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

Let’s say the current date is 2019-08-31.

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-04-21');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('2019-03-01');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('2019-09-01');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('2019-08-31');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('2019-06-30');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('2019-06-01');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------------+
| ArrivalDate |
+-------------+
| 2019-04-21  |
| 2019-03-01  |
| 2019-09-01  |
| 2019-08-31  |
| 2019-06-30  |
| 2019-06-01  |
+-------------+
6 rows in set (0.00 sec)

Following is the query to time period query −

mysql> select *from DemoTable where ArrivalDate between date_sub(curdate(),interval 14 week) and curdate();

This will produce the following output −

+-------------+
| ArrivalDate |
+-------------+
| 2019-08-31  |
| 2019-06-30  |
| 2019-06-01  |
+-------------+
3 rows in set (0.00 sec)

Updated on: 03-Oct-2019

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements