尝试运行以下内容时出现错误(1064)...(MySql 5.5.9)
查询:
CREATE TRIGGER clearChat AFTER INSERT ON chat
FOR EACH ROW
BEGIN
DELETE p.* FROM chat p LEFT JOIN (SELECT t.id FROM chat t ORDER BY t.id DESC LIMIT 50) x ON x.id = p.id WHERE x.id IS NULL
END;
错误是:
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 'END' at line 5
任何帮助都会很棒。
上次修改:已更新以显示“FOR EACH ROW”和“BEGIN”
答案 0 :(得分:3)
FOR EACH ROW
DELETE
之前您遗漏了delimiter |
CREATE TRIGGER clearChat AFTER INSERT ON chat
FOR EACH ROW BEGIN
DELETE p.* FROM chat p LEFT JOIN (SELECT t.id FROM chat t ORDER BY t.id DESC LIMIT 50) x ON x.id = p.id WHERE x.id IS NULL;
END;
|
delimiter ;
:http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
编辑:还有更多问题。正确的语法如下:
OLD
编辑2:
根据此http://dev.mysql.com/doc/refman/5.1/en/faqs-triggers.html#qandaitem-B-5-1-9,我认为根本不允许查询处于触发状态:
触发器可以访问旧的和新的 数据在自己的表中。触发器可以 也影响其他表,但它是 不允许修改那个表 已被使用(用于阅读或 通过调用的语句写) 功能或触发器。
由于您未使用NEW
或chat
,我认为您无法修改chat
,因为在{{1}}的插入时触发了触发器。
答案 1 :(得分:1)
我在下面的语句中遇到了同样的问题,它总是给我一个语法错误,即使是这个简化的删除语句(原来是DELETE FROM APP_CACHE_VIEW WHERE APP_UID = OLD.APP_UID;
):
CREATE TRIGGER APPLICATION_DELETE BEFORE DELETE ON APPLICATION
FOR EACH ROW
BEGIN
DELETE FROM APP_CACHE_VIEW;
END
如果我将SQL命令更改为以下内容然后它已经工作但我不明白为什么:
DELIMITER $$
CREATE TRIGGER APPLICATION_DELETE BEFORE DELETE ON APPLICATION
FOR EACH ROW
BEGIN
DELETE FROM APP_CACHE_VIEW;
END$$
DELIMITER ;