我已经在互联网上寻找了许多地方mysql error #1442
的原因
无法在存储的函数/触发器中更新表'unlucky_table',因为 它已被调用此存储的语句使用 功能/触发
有人说这是mysql中的一个错误或它没有提供的功能。
现在我无法理解为什么这是递归的。我有一个案例,我有2个表table1
和table2
,我运行一个SQL查询
update table1 set avail = 0 where id in (select id from table2 where duration < now() - interval 2 hour);
现在after update trigger
上的table1
为
CREATE TRIGGER trig_table1 AFTER UPDATE ON table1
FOR EACH ROW begin
if old.avail=1 and new.avail=0 then
delete from table2 where id=new.id;
end if;
现在当我执行更新查询时,我收到1442错误。 在这种情况下递归是什么?
is this error a lack of feature in mysql?
OR
does this have to do with how mysql executes queries?
OR
is there something logically wrong with executing such queries?
答案 0 :(得分:4)
更新时无法引用表格。
/* my sql does not support this */
UPDATE tableName WHERE 1 = (SELECT 1 FROM tableName)
触发器可以访问自己的表中的旧数据和新数据。触发器也可以影响其他表,但不允许通过调用函数或触发器的语句修改已经使用(用于读取或写入)的表。 (在MySQL 5.0.10之前,触发器无法修改其他表。)