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)

Updated on: 01-Oct-2019

426 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements