Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto delete Expired Data Row in MySQL

Tags:

sql

php

mysql

I have added some bonus code rows into my bonusdetails table in the database. All bonus codes have an expiry date.

Is it possible to automatically delete the row that its expiry date has reached by php? Code I'm using is(Validity is date):-

$query = "select * 
          from bonusdetails 
          where BonusType='Match Bonus' 
          order by Validity ASC limit 0,30;";

$result = mysql_query($query);

echo '<table>';
.....
.....
.....
echo '</table>';

?>
like image 600
user2077650 Avatar asked Dec 26 '22 07:12

user2077650


1 Answers

You may try to use MySQL Events for that:

CREATE EVENT IF NOT EXISTS `dbName`.`eventName`
ON SCHEDULE
EVERY 1 DAY // or 1 HOUR
COMMENT 'Description'
DO
BEGIN

DELETE FROM `dbName`.`TableName` WHERE `expireDateCol` < NOW();

END

NOTE that MySQL Event Scheduler need to be enabled on your server:

SET GLOBAL event_scheduler = ON;
like image 58
Ezhil Avatar answered Jan 06 '23 07:01

Ezhil