
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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 Articles
- Fetch records from interval of past 3 days from current date in MySQL and add the corresponding records
- 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()?
- MySQL Datetime to add days?
- Fetch student records whose result declared 12 days before the current date in MYSQL
- How to subtract 30 days from the current datetime in MySQL?
- How to subtract 10 days from the current datetime in MySQL?
- MySQL query to fetch records from a range of months?
- 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?
- MySQL DateTime Now()+5 days/hours/minutes/seconds?
- How can fetch records from specific month and year in a MySQL table?

Advertisements