- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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)
Advertisements