
- 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
Query MySQL table and fetch rows posted before the last 3 days?
Let’s say the current date is −
'2019-10-20
We will first see an example and create a table −
mysql> create table DemoTable1582 -> ( -> PostedDate datetime -> ); Query OK, 0 rows affected (13.36 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1582 values('2019-01-21 12:34:40'); Query OK, 1 row affected (1.06 sec) mysql> insert into DemoTable1582 values('2019-10-15 11:00:00'); Query OK, 1 row affected (0.87 sec) mysql> insert into DemoTable1582 values('2019-10-25 1:10:00'); Query OK, 1 row affected (1.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1582;
This will produce the following output −
+---------------------+ | PostedDate | +---------------------+ | 2019-01-21 12:34:40 | | 2019-10-15 11:00:00 | | 2019-10-25 01:10:00 | +---------------------+ 3 rows in set (0.00 sec)
Here is the query to fetch rows posted before last 3 days.
mysql> select * from DemoTable1582 -> where PostedDate between date_sub(PostedDate, interval 3 day) and now();
This will produce the following output −
+---------------------+ | PostedDate | +---------------------+ | 2019-01-21 12:34:40 | | 2019-10-15 11:00:00 | +---------------------+ 2 rows in set (0.20 sec)
- Related Articles
- MySQL query to fetch records wherein timestamp is before 15+ days?
- MySQL query to fetch records before currentdate + 2 weeks?
- Fetch random rows from a table with MySQL
- MySQL query to fetch date more recent than 14 days?
- MySQL query to find the number of rows in the last query
- Fetch student records whose result declared 12 days before the current date in MYSQL
- MySQL query to delete all rows older than 30 days?
- MySQL SELECT last few days?
- MySQL query to get the last created table name (most recent)?
- Fetch specific rows from a MySQL table with duplicate column values (names)?
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- MySQL query to fetch the latest date from a table with date records
- Select rows from a table with date between 90 days ago and now in MySQL
- MySQL query to calculate the days between two dates from different columns but similar rows
- Fetch the substring after last dot in MySQL

Advertisements