
- 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
MySQL query to select rows older than a week?
For this, you can use DATEDIFF() function. The current date time is as follows −
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-06-09 19:15:56 | +---------------------+ 1 row in set (0.00 sec)
Let us first create a table −
mysql> create table DemoTable -> ( -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.66 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-06-01'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-06-02'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('2019-06-14'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-05-21'); Query OK, 1 row affected (0.24 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+---------------------+ | ShippingDate | +---------------------+ | 2019-06-01 00:00:00 | | 2019-06-02 00:00:00 | | 2019-06-14 00:00:00 | | 2019-05-21 00:00:00 | +---------------------+ 4 rows in set (0.00 sec)
Following is the query to select rows older than a week. Let’s say the current date is “2019-06-09”. Therefore, the rows would get selected older than a week i.e. before “2019-06-02” −
mysql> select *from DemoTable where DATEDIFF(now(),ShippingDate) > 7;
Output
+---------------------+ | ShippingDate | +---------------------+ | 2019-06-01 00:00:00 | | 2019-05-21 00:00:00 | +---------------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to delete all rows older than 30 days?
- MySQL query to return all records with a datetime older than 1 week
- How to delete rows older than 14 days in MySQL?
- Deleting all rows older than 5 days in MySQL
- MySQL query to select too many rows?
- MySQL query to select multiple rows effectively?
- Selecting rows that are older than current date in MySQL?
- MySQL query to select top n rows efficiently?
- MySQL query to delete a DATE older than 30 days from another date?
- MySQL query to select rows one batch at a time
- Fetching rows updated at timestamp older than 1 day in MySQL?
- MySQL select query to select rows from a table that are not in another table?
- MySQL query to order rows with value greater than zero?
- MySQL query to select rows except first row in descending order?
- How to order results of a query randomly & select random rows in MySQL?

Advertisements