首先,我知道有很多相同类型的问题,但它们并没有解决我的问题!
如果给定的2列的行数超过10行,我想删除最旧的行。我的触发器如下所示:
DELIMITER //
CREATE TRIGGER TEST_TRIGGER
AFTER INSERT
ON table1
FOR EACH ROW
BEGIN
SELECT COUNT(*)
INTO @Tcount
FROM table1
WHERE columnA = NEW.columnA
AND columnB = NEW.columnB;
IF @Tcount > 10 THEN
DELETE FROM table1
WHERE id in (select * from (select MIN(id) from table1 where columnA = NEW.columnA and columnB = NEW.columnB) as t);
END IF;
END;
//
DELIMITER;
我在文件中有触发器,执行时会抛出错误。完整错误是
ERROR 1064 (42000) at line 25: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER' at line 1
第25行是DELIMITER;
我的表格如下:
CREATE TABLE `table1` (
`columnA` varchar(255) NOT NULL,
`columnB` varchar(255) NOT NULL,
`Version` varchar(255) NOT NULL,
`Build` int(11) DEFAULT NULL,
`Date` varchar(255) DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
)
即使抛出错误,触发器也会被添加。但是当我向表中添加第11个条目(对于给定的columnA和columnB的组合)时,我得到以下错误:
ERROR 1442 (HY000): Can't update table 'table1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
当单独执行时,触发器中的select和delete语句很好,当然还有适当的值。
MySQL版5.5.46
我非常感谢任何更正/建议。