Reputation: 461
How to create event Auto-Delete some row 1 day after inserting that row?
I have 3 fields:
id varchar
name varchar
timestamp current_timestamp()
Upvotes: 3
Views: 9188
Reputation: 195
Please take a look at Event: https://dev.mysql.com/doc/refman/5.7/en/events-syntax.html in your case: you can try this one:
-- create a timer to update this table automatically
DROP EVENT IF EXISTS `et_update_your_trigger_name`;
CREATE EVENT `et_update_your_trigger_name` ON SCHEDULE EVERY 1 MINUTE
STARTS '2010-01-01 00:00:00'
DO
DELETE FROM `DB_NAME`.`table_name` where DATEDIFF(now(),`timestamp`) > 1;
ALTER EVENT `et_update_your_trigger_name` ON COMPLETION PRESERVE ENABLE;
this sql code create a trigger, and execute every minutes.
Upvotes: 7
Reputation: 4033
Please try like this:
DELETE FROM table1
WHERE date < DATE_SUB(NOW(), INTERVAL 1 DAY)
Upvotes: 0
Reputation: 13006
Here's your scripts.
Insert into table1 (id, name, timestamp) values (1, 'test', now())
after insert
Delete from table1 WHERE timestamp < now() - interval 1 day;
Upvotes: 4