如何在一个事件中执行多个 DELETE 查询 MySQL 8
How to preform multiple DELETE queries in an event MySQL 8
我正在尝试使用事件在一段时间后删除行。这样做时,我还有其他事情要处理。我只想删除行,如果它们想在一定时间后删除。不知道怎么表达,看代码就明白了:
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';
如果我只有一个 DELETE 语句,这会起作用,但是当我一次添加多个时,该事件只接受第一个,甚至没有注意到其余的(我在创建事件后查看了它并只有一个 DELETE 语句[总是第一个])。
很抱歉,如果之前有人回答过,我不知道要搜索什么。
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');
我正在尝试使用事件在一段时间后删除行。这样做时,我还有其他事情要处理。我只想删除行,如果它们想在一定时间后删除。不知道怎么表达,看代码就明白了:
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';
如果我只有一个 DELETE 语句,这会起作用,但是当我一次添加多个时,该事件只接受第一个,甚至没有注意到其余的(我在创建事件后查看了它并只有一个 DELETE 语句[总是第一个])。
很抱歉,如果之前有人回答过,我不知道要搜索什么。
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');