【MYSQL】update/delete/select语句中的子查询

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);

另外,做查询时,能用单表就不要用多表。这样一来,逻辑清晰,出错的概率也就少多了。

原文地址:https://www.cnblogs.com/lauren1003/p/5894458.html