0

每个人。我想在插入记录后计算 A(表)中的 id 计数,我的 sql 出现故障:

CREATE TRIGGER check_data_count
AFTER
INSERT
ON A
FOR EACH ROW
BEGIN
     SELECT @data_count:=COUNT(*) FROM A WHERE id = NEW.id;
END;

如我们所见,我犯了一些错误(^_^)。

我的 mysql 版本信息:Ver 14.14 Ditrib 5.5.9,适用于 Win64(X86)。错误消息是:

Error Code : 1064
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 '' at line 8

Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000
---------------------------------------------------

Query : end

Error Code : 1064
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 1

Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

最后,谢谢大家。

4

2 回答 2

0

无法对触发触发器的同一个表执行选择、更新、删除操作。

来自MySQL 参考手册

B.5.9: Can triggers access tables?

A trigger can access both old and new data in its own table. A trigger can also affect other tables, but it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
于 2013-04-18T09:26:55.963 回答
0

您忘记在CREATE TRIGGER查询中使用分隔符:

DELIMITER $$
CREATE TRIGGER check_data_count AFTER INSERT ON A
FOR EACH ROW BEGIN
 SELECT @data_count:=COUNT(*) FROM A WHERE id = NEW.id;
END$$
DELIMITER ;
于 2013-04-18T10:00:01.780 回答