mysql的You can't specify target table '***' for update in FROM clause
student表
-
目的sql语句
查询student表中年龄最大的,并将它的name改为"十五岁"
- 第一时间我想到的sql语句是
update 表名 set name = 十五岁
where age = ( select max(age) from 表名)
结果运行时报异常:You can't specify target table 'student' for update in FROM clause
这个异常的意思就是
第二层查询的FROM子句中的表,不能作为更新的目标表。即不能对同一张表的查询结果作为本表增删改的判断条件
- 那么接下来我就想到,讲查询结果设置成中间表,然后进行where的判断
update 表名 set name = 十五岁
where age =( select t.age from ( select max(age) as num from 表名) t)
总结
即不能对一张表的查询结果作为本表增删改的判断条件
select出的结果再通过中间表select一遍,这样就规避了错误。