sql面试题02

删除表中的重复数据

学生表如下:stu
自动编号 学号    姓名 课程编号  课程名称 分数
1        2005001 张三 0001      数学     69
2        2005002 李四 0001      数学     89
3        2005001 张三 0001      数学     69
删除除了自动编号不同, 其他都相同的学生冗余信息
delete from stu where id not in(
select min(id) from stu group by 学号,姓名,课程编号,课程名称,分数
)
这个sql在mysql中执行报错


//查询不需要删除的id,存储在一张临时表中
create table temp as select min(id) as not_id from stu group by 学号,姓名,课程编号,课程名称,分数
//删除stu表中的重复数据
delete from stu where id not in(
select not_id from temp
)
//删除临时表
drop table temp


猜你喜欢

转载自blog.csdn.net/wdhouyigege/article/details/80737156