top button
Flag Notify
Site Registration

How to delete old records in my MySql database?

0 votes

I wish to delete records in my database that is older than 3 months.

$todays_date = date('Y-m-d');
$old_records_to_delete = ???

$sql = "DELETE FROM table WHERE date >= '$old_records_to_delete'";
mysql_query($sql, $connect_db) or die(mysql_error());

posted Aug 2, 2013 by Deepak Dasgupta

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote
Best answer

$query = "DELETE FROM __table_name__ WHERE __date__ < NOW() - INTERVAL 3 MONTH"

answer Aug 2, 2013 by Jagan Mishra
+1 vote

$query = "DELETE FROM __table_name__ WHERE __date__ BETWEEN NOW() - INTERVAL 3 MONTH AND NOW()"

answer Aug 2, 2013 by Sheetal Chauhan
0 votes

Try something like:

$oldDate = new DateTime();
$oldDate->sub(new DateInterval('P3M'));
$old_records_to_delete = $oldDate->format('Y-m-d');

answer Aug 2, 2013 by Abhay Kulkarni
Similar Questions
–1 vote

I want to update my sql database record and want to populate the value of previous record. So anybody can help me to get the previous value of the record to show when the update require.

+1 vote

I have some problem
I have store the data in the database without encrypt but i am view the data encrypt method after 1 day and doesn't know the details.

I am using php and mysql, Could the experts please comment on this, and offer some advice?