删除学生表中信息,使得学生的姓名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);
本文介绍了一种在MySQL数据库中删除具有重复name和kid字段的学生记录的方法,避免了You can't specify target table for update in FROM clause错误。通过使用子查询和中间表,成功实现了联合唯一约束的维护。

203

被折叠的 条评论
为什么被折叠?



