you can't specify target table for update in from

asset表裏ip相同的字段有多個,如今要完成一個功能是,根據一個ip,得到任意一個id,當asset_extension表裏department=0時,才根據該id更新asset_extension表裏 的department爲1:ip

update asset_extension t1 set t1.department=4 where t1.asset_id=(select t2.id from asset t2 join asset_extension t1 on t2.id=t1.asset_id where t1.department=0 and t2.ip='192.168.58.8' group by t2.ip)ci

結果運行時報錯:you can't specify  target table 't1' for update in from clause.get

改爲下面這樣便可,即中間增長一個過渡,不要直接在t1表時同時查詢和更新:io

update asset_extension t1 set t1.department=4 where t1.asset_id=(select * from(select t2.id from asset t2 join asset_extension t1 on t2.id=t1.asset_id where t1.department=0 and t2.ip='192.168.58.8' group by t2.ip) as subtable);table

相關文章
相關標籤/搜索