You can't specify target table 'sc' for update in FROM clausemysql
背景:把「sc」表中「葉平」老師教的課的成績都更改成此課程的平均成績; sql
上面的sql是我寫的,執行就報這個錯,這個緣由說的是 不能從本身表裏查數據再更新本身spa
解決方法:嵌套一層中間表code
update sc set sc.score = (select t1.score from (select avg(sc1.score) score from sc sc1 where sc1.c_id=(select course.c_id from teacher,course where course.t_id=teacher.t_id and tname='葉平')) t1) where sc.c_id = (select course.c_id from teacher,course where course.t_id=teacher.t_id and tname='葉平');
這種問題僅存在於mysql中,在Oracle中是不存在的,所以在此打個mark 以防下次又忘記了blog