

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fetch datetime row from exactly past 7 days records in MySQL
For this, you can use the INTERVAL 7 day concept. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, AdmissionDate datetime ); Query OK, 0 rows affected (0.83 sec)
Note − Let’s say the current date is 2019-08-23.
Insert some records in the table using insert command −
mysql> insert into DemoTable(AdmissionDate) values('2019-01-23'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-15'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-16'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-08-24'); 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 −
+----+---------------------+ | Id | AdmissionDate | +----+---------------------+ | 1 | 2019-01-23 00:00:00 | | 2 | 2019-08-15 00:00:00 | | 3 | 2019-08-16 00:00:00 | | 4 | 2019-08-24 00:00:00 | +----+---------------------+ 4 rows in set (0.00 sec)
Here is the query to fetch DateTime row from exactly past 7 days records −
mysql> select *from DemoTable where date(AdmissionDate)=CURDATE() - interval 7 day;
This will produce the following output −
+----+---------------------+ | Id | AdmissionDate | +----+---------------------+ | 3 | 2019-08-16 00:00:00 | +----+---------------------+ 1 row in set (0.04 sec)
- Related Questions & Answers
- Fetch records from interval of past 3 days from current date in MySQL and add the corresponding records
- MySQL Datetime to add days?
- MySQL query to fetch records wherein timestamp is before 15+ days?
- Fetch similar ID records from two tables in MySQL
- Fetch records from comma separated values using MySQL IN()?
- How to subtract 30 days from the current datetime in MySQL?
- How to subtract 10 days from the current datetime in MySQL?
- Fetch student records whose result declared 12 days before the current date in MYSQL
- MySQL DateTime Now()+5 days/hours/minutes/seconds?
- MySQL query to fetch records from a range of months?
- Selecting records 15 days before today in MySQL?
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- How can we fetch alternate even-numbered records from MySQL table?
- How can we fetch alternate odd numbered records from MySQL table?
- How to fetch the newly added records from a MySQL table?
Advertisements