How to get the date between TODAY and TODAY-7”?


To get the date between dates, you need to use. Here, we are getting the dates between today and today-7 days −

select *from yourTableName where DATE(yourColumnName) > (NOW() - INTERVAL 7 DAY);

Note : Let’s say the current date is '2019-06-02’ Let us first create a table.

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2018-03-21');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('2019-05-22');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('2019-05-27');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('2019-06-01');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+------------+
| LoginDate  |
+------------+
| 2018-03-21 |
| 2019-05-22 |
| 2019-05-27 |
| 2019-06-01 |
+------------+
4 rows in set (0.00 sec)

Following is the query to display dates between today and today – 7 days −

mysql> select *from DemoTable where DATE(LoginDate) > (NOW() - INTERVAL 7 DAY);

Output

+------------+
| LoginDate  |
+------------+
| 2019-05-27 |
| 2019-06-01 |
+------------+
2 rows in set (0.06 sec)
raja
Published on 13-Jun-2019 11:31:47
Advertisements