mysql1442错误,mysql错误1442的真正原因是什么?

well i have looked for a lot of places on the internet for the cause of the mysql error #1442 which says

Can't update table 'unlucky_table' in stored function/trigger because

it is already used by statement which invoked this stored

function/trigger

some say that this is a bug in mysql or a feature that it doesnt provide.

now i cant understand why this is recursive. i have a case in which i have 2 tables table1 and table2 and i run an sql query as

update table1 set avail = 0 where id in (select id from table2 where duration < now() - interval 2 hour);

now i have an after update trigger on table1 as

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;

now when i execute the update query i get a 1442 error.

whats recursive in this case?

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?

解决方案

You cannot refer to a table when updating it.

/* my sql does not support this */

UPDATE tableName WHERE 1 = (SELECT 1 FROM tableName)

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. (Before MySQL 5.0.10, a trigger cannot modify other tables.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值