mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。 例如下面这个sql:
--15、删除学习“叶平”老师课的SC表记录;
delete from score
where score.course_id in
(select course.cid as c_id from score t_score
left join course on course.cid=t_score.course_id
left join teacher on teacher.tid=course.teacher_id
where teacher.tname like '李平老师'
group by course.cid);
改写成下面就行了:
--15、删除学习“叶平”老师课的SC表记录;
delete from score
where score.course_id in
(SELECT c_id from
(select course.cid as c_id from score t_score
left join course on course.cid=t_score.course_id
left join teacher on teacher.tid=course.teacher_id
where teacher.tname like '李平老师'
group by course.cid)as s);
也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。注意,这个问题只出现于mysql,mssql和oracle不会出现此问题。