MySQL报错:1093 - You can't specify target table 'stu' for update in FROM clause

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/b509_ying/article/details/89313879

删除学生表中信息,使得学生的姓名name和kid是联合唯一的

sql语句:delete from stu where id not in (select min(id) from stu group by name,kid);

MySQL数据库执行该sql语句时,出现错误:1093 - You can't specify target table 'stu' for update in FROM clause,如下图:

错误:意思是不能在同一语句中更新select出的同一张表元组的属性值

解决方法:将select出的结果通过中间表再select一遍即可。

修改后的语句如下:

delete from stu where id not in (select min(a.id) from (select * from stu) a group by a.name,a.kid);

猜你喜欢

转载自blog.csdn.net/b509_ying/article/details/89313879