1.什么是重复数据行:
1>.两笔记录的每一个字段都相同(表中没有主键);
2>.两笔记录的特定字段均相同,其余字段不同(表中可能有主键,也可能没有主键);
2.如何删除重复数据行:
1>.对于两笔记录完全相同的情形:
a.方法一(建临时表):
select distinct * into table2 from table1
drop table talbe1
select * into table1 from table2
drop table table2
b.方法二(用自己去union自己,这种方法在数据量较大时,速度会比较慢):
select * into table2 from (select * from table1 as t1 UNION select * from table1 as t2)
drop table talbe1
select * into table1 from table2
drop table table2
2>.对于特定字段相同的情形:
a.方法一(有无主键均适用):先取行号,再删除其中行号非最小者或者非最大者(看个人喜好);
alter table table1 add `_AutoID_` AUTOINCREMENT(1,1)
delete from table1 where `_AutoID_` not in (select min(`_AutoID_`) as MinID from table1 group by Field1,Field2,...Fieldn)
alter table table1 drop column `_AutoID_`
b.方法二(若有主键,可按主键,再加上自己的逻辑判断加以删除)
delete from table1 where _PK not in (select min(_PK) as MinID from table1 group by Field1,Field2,...Fieldn)
3.实际操作:
1>.原始数据:
2>.添加自动编号列:
3>.删除重复数据:
4>.删除自动编号列,以达到不影响数据表结构的原则:
5>.最终删除了重复数据之后的结果: