
- 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 a DATE older than 30 days from another date?
Following is the syntax −
delete from yourTableName where yourColumnName < (yourAnotherDateValue - INTERVAL 30 DAY);
Let us first create a table −
mysql> create table DemoTable ( DueDate date ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-08-25'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('2019-07-01'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-06-20'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('2019-09-02'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+ | DueDate | +------------+ | 2019-08-25 | | 2019-07-01 | | 2019-06-20 | | 2019-09-02 | +------------+ 4 rows in set (0.00 sec)
Following is the query to delete a DATE that is older than 30 days from another date −
mysql> delete from DemoTable where DueDate < ('2019-08-31' - INTERVAL 30 DAY); Query OK, 2 rows affected (0.22 sec)
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+------------+ | DueDate | +------------+ | 2019-08-25 | | 2019-09-02 | +------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to delete all rows older than 30 days?
- MySQL query to fetch date more recent than 14 days?
- How to delete rows older than 14 days in MySQL?
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- Add 30 days to date in a MySQL table with arrival date records
- MySQL query to find expiry date (record) from the next 2 days?
- MySQL query to count days in date range with start and end date
- Selecting rows that are older than current date in MySQL?
- MySQL add days to a date?
- MySQL query to select date from 00:00 to today’s date
- MySQL query to select date from timestamp?
- MySQL query to fetch the latest date from a table with date records
- MySQL Query to convert from datetime to date?
- MySQL query to select closest date from today?
- MySQL query to select rows older than a week?

Advertisements