数据库报错提示内容
You can’t specify target table ‘student’ for update in FROM clause
学生表student
查询年龄最大的一个并将年龄加100
错误写法
update student set age = age+100 where age = (select max(age) from student )
原因: 这是因为在查询最大值的时候已经有事务,将表的已经锁住,本次操作不可执行DML操作,故而报错
解决方法
update student set age = age + 100 where age = (select age from (select max(age) from student) c)
说明:这是将原来的查询内容的基础上在进行查询一次,然后在进行DML操作,这可保证未直接对查询的内容进行DML操作,保证了事务的一致性。