Delete MySQL Rows Older Than Date

I ran into a problem recently where I needed to delete all rows in a MySQL table that were older than three months. This I was able to accomplish with the following statement:

DELETE FROM [table] WHERE [column] < DATE_SUB(NOW(), INTERVAL 3 MONTH);

Substitute table and column, and a proper date option. The date_sub function subtracts a time period from the date specified. In this statement, NOW() grabs the current date from which INTERVAL 3 MONTHS is subtracted (along with the < operand).



1 Comment so far

  1. MySQL Tutorials @ November 6th, 2008

    MySQL Tutorials…

    The updates were performed in such a way that new Web server connections were delayed during the 30 seconds or so that PHP and MySQL were unavailable on each server. That should mean that as far as scripts on your Web site were concerned, there was zer…

Leave a reply

Add this site to your Firefox Search Bar

Twitter Updates


First blue moon on NYE in almost 20 years:

Follow Ben on Twitter

Recent Entries

Categories

Archives

This site is optimized for Firefox.