

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- MySQL query to delete all rows older than 30 days?
- How to delete rows older than 14 days in MySQL?
- Add 30 days to date in a MySQL table with arrival date records
- MySQL query to fetch date more recent than 14 days?
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- Selecting rows that are older than current date in MySQL?
- MySQL add days to a date?
- MySQL query to count days in date range with start and end date
- MySQL query to find expiry date (record) from the next 2 days?
- MySQL query to select rows older than a week?
- Deleting all rows older than 5 days in MySQL
- MySQL query to select date from timestamp?
- Find posts that are older than current date in MongoDB?
- MySQL query to fetch the latest date from a table with date records
- Show date like 30-04-2020 instead of 2020-04-30 from MySQL database?
Advertisements