mysql uodate 报错 You can't specify target table '**' for update in FROM clause

 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

相关文章
相关标签/搜索