我只想保留表中最后5条记录。所以我打算创建一个触发器(在mytable上),在插入一条新记录之后,它会删除除最后5条记录之外的所有记录。当我尝试创建此触发器时:
DELIMITER //
CREATE TRIGGER DeleteOldRecords AFTER INSERT ON MyTable FOR EACH ROW
BEGIN
DELETE FROM MyTable WHERE id NOT IN
(SELECT id FROM MyTable ORDER BY id DESC LIMIT 5);
END//
我收到以下错误消息: Error Code: 1235. This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
. 我使用mysql v5.7.14
如果我像这样将in子句加密到子查询中:
DELIMITER //
CREATE TRIGGER DeleteOldRecords AFTER INSERT ON MyTable FOR EACH ROW
BEGIN
DELETE FROM MyTable WHERE id NOT IN
(SELECT id FROM (SELECT id FROM MyTable ORDER BY id DESC LIMIT 5) x);
END//
我可以创建这样一个触发器,但现在我无法将记录插入到表中 MyTable
. 我收到以下错误消息: Error Code: 1442. Can't update table 'MyTable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
解决办法是什么(我不想要删除旧记录的cron作业)
暂无答案!
目前还没有任何答案,快来回答吧!