Be solved using MySQL error when update: You can not specify target table 'table name' for update in FROM clause

Error when using MySQL conduct update:
MySQL in You can not specify target table 'table name' for update in FROM clause
Translation: You can not select a certain value to the same table, and then update the table (in the same statement) that can not be done based on the value of a field judge again to update the value of a field.

出现问题语句:
update sc set score=(select avg(sc.score) from sc,teacher,course where teacher.t=course.t and sc.c=course.c and teacher.tname=‘叶平’) where sc.c in(select sc.c from teacher,course,sc where teacher.t=course.t and sc.c=course.c and teacher.tname=‘叶平’);

Solution:
The SELECT the results through the middle of the table SELECT again, thus avoiding the error. The best is to specify the column name plus later select distinct or limit.

解决后语句:
update sc set score=(select a.t from (select distinct avg(sc.score) t from sc,teacher,course where teacher.t=course.t and sc.c=course.c and teacher.tname=‘叶平’) a) where sc.c in(select b.tt from (select distinct sc.c tt from teacher,course,sc where teacher.t=course.t and sc.c=course.c and teacher.tname=‘叶平’) b );

Published an original article · won praise 2 · views 21

Guess you like

Origin blog.csdn.net/qq_37255858/article/details/104751207