在执行如下更新语句的时候,提示如下问题: You can't specify target table 'outorder' for update in FROM clause 0.000 sec, 大体翻译出来的意思是: 不能先select出同一表中的某些值,再update这个表(在同一语句中)
update outorder set SchoolId = 5,canteenid = 4 where BillId in ( select BillId from outorder where CanteenId = 6 );
解决方案:将查询出来的数据集放在一个表中,类似如下语句
SELECT a.BillId FROM (select BillId from outorder where CanteenId = 6) a
最终的语句调整为如下格式
update outorder set SchoolId = 5,canteenid = 4 where BillId in ( SELECT a.BillId FROM (select BillId from outorder where CanteenId = 6) a );