Search code examples
mysqlmysql-8.0mysql-event

How to preform multiple DELETE queries in an event MySQL 8


I am trying to delete rows after a certain period of time, using an event. When doing this I have other things to take care of.. I only want to delete rows if they want to be deleted after a certain amount of time. Don't really know how to phrase that, but you'll understand when you look at the code:

CREATE EVENT IF NOT EXISTS `remove_links`
ON SCHEDULE EVERY 1 SECOND
ON COMPLETION PRESERVE
ENABLE
DO 

DELETE FROM links WHERE time < (NOW() - INTERVAL 1 DAY) AND EXPIRE = 'A Day';
DELETE FROM links WHERE time < (NOW() - INTERVAL 1 WEEK) AND EXPIRE = 'A Week';
DELETE FROM links WHERE time < (NOW() - INTERVAL 1 MONTH) AND EXPIRE = 'A Month';

This works if I only had one DELETE statement, but when I add in multiple at a time, the event just accepts the first one and doesn't even notice the rest (I looked at the event after creating it and there was only one DELETE statement [always the first one] ).

I'm sorry if this has been answered before, I didn't know what to search for.


Solution

  • DELETE FROM links WHERE (time < (NOW() - INTERVAL 1 DAY) AND EXPIRE = 'A Day') or 
    ( time < (NOW() - INTERVAL 1 WEEK) AND EXPIRE = 'A Week') or
    (time < (NOW() - INTERVAL 1 MONTH) AND EXPIRE = 'A Month');