
- 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 return all records with a datetime older than 1 week
To get dates older than 1 week, you can use the following syntax −
select *from yourTableName where yourColumnName < now() - interval 1 week;
To understand the above concept, let us create a table. The query to create a table is as follows −
mysql> create table DatesOfOneWeek −> ( −> ArrivalTime datetime −> ); Query OK, 0 rows affected (0.87 sec)
Insert some records in the table −
mysql> insert into DatesOfOneWeek values(date_add(now(),interval 2 week)); Query OK, 1 row affected (0.11 sec) mysql> insert into DatesOfOneWeek values('2018-11-04'); Query OK, 1 row affected (0.14 sec) mysql> insert into DatesOfOneWeek values('2018-11-25'); Query OK, 1 row affected (0.11 sec) mysql> insert into DatesOfOneWeek values(date_add(now(),interval -1 week)); Query OK, 1 row affected (0.14 sec) mysql> insert into DatesOfOneWeek values(date_add(now(),interval 1 week)); Query OK, 1 row affected (0.11 sec)
Let us check the records which we have inserted above are present or not. The query to display all records from the table is as follows −
mysql> select *from DatesOfOneWeek;
The following is the output −
+---------------------+ | ArrivalTime | +---------------------+ | 2018-12-20 18:11:02 | | 2018-11-04 00:00:00 | | 2018-11-25 00:00:00 | | 2018-11-29 18:11:40 | | 2018-12-13 18:11:46 | +---------------------+ 5 rows in set (0.00 sec)
Here is the MySQL query to get a date which was in the past i.e. all the date before 1 week −
mysql> select *from DatesOfOneWeek where ArrivalTime < now() - interval 1 week;
The following is the output −
+---------------------+ | ArrivalTime | +---------------------+ | 2018-11-04 00:00:00 | | 2018-11-25 00:00:00 | | 2018-11-29 18:11:40 | +---------------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to select rows older than a week?
- MySQL query to delete all rows older than 30 days?
- MySQL query to subtract date records with week day and display the weekday with records
- Return all ages records that are ints with a MongoDB query
- MySQL SELECT query to return records with specific month and year
- MySQL query to return multiple row records with AND & OR operator
- Delete records where timestamp older than 5 minutes in MySQL?
- MySQL query to return 5 random records from last 20 records?
- Write a MySQL query where length of the records is longer than 1?
- MySQL query to delete a DATE older than 30 days from another date?
- Deleting all rows older than 5 days in MySQL
- MySQL query to select records with a particular date?
- MySQL query to return all items in a single row
- Use UNION ALL to insert records in two tables with a single query in MYSQL
- MySQL TINYINT type to return 1 or IS NULL records

Advertisements