mysql error 1093(HY000) You can‘t specify target table ‘xx‘ for update in FROM clause

错误代码:

update sc set grade = grade * 1.1
where sno in(select sno from sc group by sno having avg(grade)>=75);

报错详情

 You can't specify target table 'sc' for update in FROM clause
该报错意思如下:
	不能在update更新一张表的时候,同时在子查询中使用(查询 / 筛选)该表。

解决方法

设立双层子查询,让程序的执行逻辑产生先后顺序,以便正常运行,写法如下:

update sc set grade = grade * 1.1
where sno in(select * from (select sno from sc group by sno having avg(grade)>=75)as temp);

你可能感兴趣的:(mysql,mysql,数据库,sql)