Wednesday, March 6, 2013

MySQL: How to delete records by date

Delete records where date is less than a certain date:
DELETE FROM `tablename` WHERE `date` < '2012-08-01';

You can also include intervals:
DELETE FROM `tablename` WHERE `date`  < DATE_SUB(NOW(), INTERVAL 3 MONTH);
DELETE FROM `tablename` WHERE `date`  < DATE_SUB(NOW(), INTERVAL 3 WEEK);
DELETE FROM `tablename` WHERE `date`  < DATE_SUB(NOW(), INTERVAL 2 DAY);