报错信息
You can’t specify target table ‘sc’ for update in FROM clause
原sql语句
update sc set score = (select avg(sc.score) from teacher t, sc, course where t.tname = ‘叶平’ and t.t = course.t
and course.c = sc.c) where c = (select sc.c from teacher t, sc, course where t.tname = ‘叶平’ and t.t = course.t
and course.c = sc.c)
问题原因是 不能先select出同一表中的某些值,再update这个表(在同一语句中)
正确的sql:
UPDATE sc SET score = (SELECT tt.score FROM (SELECT AVG(SC_2.score) score FROM teacher t, sc SC_2, course WHERE t.tname = ‘叶平’ AND t.t = course.t
AND course.c = SC_2.c) tt ) WHERE c = (SELECT scor.c FROM (SELECT DISTINCT SC_2.c FROM teacher t, sc SC_2, course WHERE t.tname = ‘叶平’ AND t.t = course.t
AND course.c = SC_2.c)scor ) ;
对有问题的select语句进行包装
UPDATE sc SET score = (SELECT tt.score FROM (SELECT AVG(SC_2.score) score FROM teacher t, sc SC_2, course WHERE t.tname = ‘叶平’ AND t.t = course.t
AND course.c = SC_2.c) tt )