Automatically delete a record from a table after a time interval

1

How do I stop the log after a time interval, such as in a shopping cart, for example?

    
asked by anonymous 18.12.2017 / 13:29

1 answer

0

I use Scheduled Tasks with MySQL's own CREATE EVENT command. Unofficial reference of the Event Scheduler here .

Reference example above:

DELIMITER $$

CREATE 
    EVENT 'archive_blogs' 
    ON SCHEDULE EVERY 1 WEEK STARTS '2011-07-24 03:00:00' 
    DO BEGIN

    -- copy deleted posts
    INSERT INTO blog_archive (id, title, content) 
    SELECT id, title, content
    FROM blog
    WHERE deleted = 1;

    -- copy associated audit records
    INSERT INTO audit_archive (id, blog_id, changetype, changetime) 
    SELECT audit.id, audit.blog_id, audit.changetype, audit.changetime 
    FROM audit
    JOIN blog ON audit.blog_id = blog.id
    WHERE blog.deleted = 1;

    -- remove deleted blogs and audit entries
    DELETE FROM blog WHERE deleted = 1;

END */$$

DELIMITER ;

Note: the resource must be enabled through the command SET GLOBAL event_scheduler = ON;

MySQL official documentation: link

    
18.12.2017 / 14:30