
- 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 delete all rows older than 30 days?
To delete all rows older than 30 days, you need to use the DELETE with INTERVAL. Use < now() i.e. less than operator to get all the records before the current date.
Let us first create a table −
mysql> create table DemoTable -> ( -> UserMessage text, -> UserMessageSentDate date -> ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Hi','2019-06-01'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Hello','2019-07-02'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Awesome','2019-05-04'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Good','2019-01-10'); Query OK, 1 row affected (0.35 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+-------------+---------------------+ | UserMessage | UserMessageSentDate | +-------------+---------------------+ | Hi | 2019-06-01 | | Hello | 2019-07-02 | | Awesome | 2019-05-04 | | Good | 2019-01-10 | +-------------+---------------------+ 4 rows in set (0.00 sec)
Following is the query to delete all rows older than 30 days −
mysql> delete from DemoTable where UserMessageSentDate < now() - interval 30 DAY; Query OK, 3 rows affected (0.11 sec)
Let us check table records once again −
mysql> select *from DemoTable;
Output
+-------------+---------------------+ | UserMessage | UserMessageSentDate | +-------------+---------------------+ | Hello | 2019-07-02 | +-------------+---------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to delete a DATE older than 30 days from another date?
- How to delete rows older than 14 days in MySQL?
- Deleting all rows older than 5 days in MySQL
- MySQL query to select rows older than a week?
- MySQL query to return all records with a datetime older than 1 week
- Delete records where timestamp older than 5 minutes in MySQL?
- Selecting rows that are older than current date in MySQL?
- MySQL query to find all rows where string contains less than four characters?
- Fetching rows updated at timestamp older than 1 day in MySQL?
- How to delete all rows except some in MySQL?
- MySQL query to fetch date more recent than 14 days?
- MySQL query to order rows with value greater than zero?
- MySQL query to delete table rows if string in cell is matched
- MySQL query to delete row
- Query MySQL table and fetch rows posted before the last 3 days?

Advertisements