fightstarr20
fightstarr20

Reputation: 12608

SQL - Calculate 30 days ago and delete

I am trying to clear a MySQL database of entries in a table that are older than 30 days.

I think I am correct by doing it like this...

DELETE from wp_rg_lead_detail WHERE date_created < '11/05/18'

But is there a way I can get the SQL statement to calculate 30 days ago automatically? That way I could set a cronjob up and have it run everyday without me having to manually enter the date.

Upvotes: 0

Views: 48

Answers (2)

under
under

Reputation: 3067

Use date_add and sysdate functions to calculate date dynamically.

 Date_add(sysdate(), interval -30 days)

Upvotes: 1

sticky bit
sticky bit

Reputation: 37472

Use curdate() to get the current date and date_add() to subtract 30 days from it.

date_add(curdate(), INTERVAL -30 DAY);

Upvotes: 2

Related Questions