删除表中某字段重复的记录(只保留一条)
2015-03-07 14:01
232 查看
表people
注:此处默认重复字段为name,测试环境为MySQL5.6.
方法一
思路:
1、以名字来分组,找出各组记录中id最小的记录的id;
2、删除原表中id不在1步中id集合中的
delete from people
where peo_id not in
(select * from
(select min(peo_ID)
from people a
group by a.name ) as b
);
方法二
思路:
1、找出name重复记录中的最小id;
2、找出非重复记录的id;
3、将原表中id不在1和2步中的记录删除。
delete from people
where peo_id not in
(select * from
(select min(peo_id)
from people a
group by a.name
having count(1) > 1) as b)
and
peo_id not in
( select * from
(select min(peo_id)
from people a
group by a.name
having count(1) = 1) as c);
方法三
思路:
1、创建新表temp;
create table temp as select * from people where 1 != 1;
2、将合适的数据存储到temp;
insert into temp
select * from people where peo_id in
(select * from
(select min(peo_id)
from people
group by name ) as b);
3、将原表中的数据全部删除;
delete from people;
4、将temp中的数据插入到原表中。
insert into people
select * from temp;
注:此处默认重复字段为name,测试环境为MySQL5.6.
方法一
思路:
1、以名字来分组,找出各组记录中id最小的记录的id;
2、删除原表中id不在1步中id集合中的
delete from people
where peo_id not in
(select * from
(select min(peo_ID)
from people a
group by a.name ) as b
);
方法二
思路:
1、找出name重复记录中的最小id;
2、找出非重复记录的id;
3、将原表中id不在1和2步中的记录删除。
delete from people
where peo_id not in
(select * from
(select min(peo_id)
from people a
group by a.name
having count(1) > 1) as b)
and
peo_id not in
( select * from
(select min(peo_id)
from people a
group by a.name
having count(1) = 1) as c);
方法三
思路:
1、创建新表temp;
create table temp as select * from people where 1 != 1;
2、将合适的数据存储到temp;
insert into temp
select * from people where peo_id in
(select * from
(select min(peo_id)
from people
group by name ) as b);
3、将原表中的数据全部删除;
delete from people;
4、将temp中的数据插入到原表中。
insert into people
select * from temp;
相关文章推荐
- sql 删除表中多余的重复记录(多个字段),只保留一条记录
- 如何使用SQL删除某个字段重复的记录,保留其中一条
- 【转】SQL删除某些字段重复的记录(只保留一条)
- SqlServer-删除某一个字段重复的记录,只保留一条
- SQL删除某些字段重复的记录(只保留一条)
- sql 多个字段分组,删除重复记录,保留ID最小的一条
- sql 删除表中多余的重复记录(多个字段),只保留一条记录
- mysql 查找重复的字段,并删除记录只保留一条
- mysql,oracle里删除重复记录,只保留一条(除了自增长字段,其他的字段完全相同为相同记录)
- sql 删除表中多余的重复记录(多个字段),只保留一条记录
- SQL删除某些字段重复的记录(只保留一条)
- MySQL按某些字段删除重复记录,只保留一条记录
- SQL删除重复记录,只保留一条数据。
- 如何删除表中的重复记录只保留其中一条?
- 如何删除表中的重复记录只保留其中一条?
- sql2005,删除某个字段重复的记录,随机留下一条记录
- mysql中删除重复记录,只保留一条
- 字段查找如何去掉数据库重复记录并且只保留一条记录
- Sql server 删除重复记录的SQL语句保留一条
- 在oracle数据库表中删除所有重复的数据,仅保留一条记录