
- 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
How to get the records of the last two days from the current date in MySQL?
To get the records of the last days from the current date, you need to use DATE_SUB(). We will also use NOW() to get the current date. The syntax for the same is as follows
SELECT *FROM yourTableName WHERE yourDateTimeColumnName BETWEEN DATE_SUB(DATE(NOW()), INTERVAL 2 DAY) AND DATE_SUB(DATE(NOW()), INTERVAL 1 DAY);
Let us see an example
mysql> create table get2daysAgoDemo - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > Name varchar(20), - > PostDateTime datetime - > ); Query OK, 0 rows affected (1.70 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into get2daysAgoDemo(Name,PostDateTime) values('Larry',now()); Query OK, 1 row affected (0.68 sec) mysql> insert into get2daysAgoDemo(Name,PostDateTime) values('Mike','2019-02-13'); Query OK, 1 row affected (0.12 sec) mysql> insert into get2daysAgoDemo(Name,PostDateTime) values('Sam','2019-01-31'); Query OK, 1 row affected (0.15 sec) mysql> insert into get2daysAgoDemo(Name,PostDateTime) values('Bob','2019-02-14'); Query OK, 1 row affected (0.29 sec) mysql> insert into get2daysAgoDemo(Name,PostDateTime) values('David','2019-02-12'); Query OK, 1 row affected (0.21 sec) mysql> insert into get2daysAgoDemo(Name,PostDateTime) values('Carol','2019-02-11'); Query OK, 1 row affected (0.22 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from get2daysAgoDemo;
The following is the output
+----+-------+---------------------+ | Id | Name | PostDateTime | +----+-------+---------------------+ | 1 | Larry | 2019-02-15 21:47:10 | | 2 | Mike | 2019-02-13 00:00:00 | | 3 | Sam | 2019-01-31 00:00:00 | | 4 | Bob | 2019-02-14 00:00:00 | | 5 | David | 2019-02-12 00:00:00 | | 6 | Carol | 2019-02-11 00:00:00 | +----+-------+---------------------+ 6 rows in set (0.00 sec)
Here is the query to get the records of the last two days. Let’s say the current date is 2019-02-15
mysql> select *from get2daysAgoDemo - > where PostDateTime between DATE_SUB(DATE(NOW()), INTERVAL 2 DAY) - > AND DATE_SUB(DATE(NOW()), INTERVAL 1 DAY);
The following is the output displaying the records from the last 2 days i.e. 13th and 14th February sine our current date is 15th February
+----+------+---------------------+ | Id | Name | PostDateTime | +----+------+---------------------+ | 2 | Mike | 2019-02-13 00:00:00 | | 4 | Bob | 2019-02-14 00:00:00 | +----+------+---------------------+ 2 rows in set (0.01 sec)
- Related Articles
- Fetch records from interval of past 3 days from current date in MySQL and add the corresponding records
- How to get the difference between date records and the current date in MySQL?
- MySQL query to get the current date records wherein one of the columns displays current date
- Comparison of varchar date records from the current date in MySQL
- How to compare the first date and the last date from a MySQL column with date records?
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- How to find last date from records with date values in MySQL?
- Fetch student records whose result declared 12 days before the current date in MYSQL
- Get the number of days between current date and date field?
- MySQL query to get the current date from the list of dates
- Get the last days of all the months in MySQL?
- How to display first day and last day of the month from date records in MySQL?
- Display records from the current date till rest of the same month in MySQL?
- Find the difference between current date and the date records from a MySQL table
- How to get last day of the current month in MySQL?

Advertisements