update或delete語句里含有子查詢時,子查詢里的表不能在update或是delete語句中,如含有運行時會報錯;但select語句里含有子查詢時,子查詢里的表可以在select語句中。
如:把總成績小於100的學生名稱修改為天才
select stu_id from score group by stu_id having sum(grade)<100; #查詢總成績小於100的學生ID
update students set name='天才' where id in (select stu_id from score group by stu_id having sum(grade)<100);
另外,做查詢時,能用單表就不要用多表。這樣一來,邏輯清晰,出錯的概率也就少多了。