数据库表去除重复数据

方法一:

//创建一个临时表

create table temp_table as select * from old_table;

//清除原先表的数据 (truncate与delete的区别:前者速度快,但是删除后,没法回滚,没法触发触发器)

truncate table old_table;

//重复数据只显示第一条数据,把处理过后,数据唯一的数据插入到原先表里面

insert into old_table(id,字段1,字段2) (select min(id),字段1,字段2 from temp_table group by 字段1,字段2);

//删除临时表

drop table temp_table;

方法二:

扫描二维码关注公众号,回复: 2451711 查看本文章

//直接删除表格中多余的数据  (id是唯一值,如果没有唯一值的字段,可以用rowid来替换,注意:rowid只能用于oracle)

delete from old_table a where (a.字段1,a.字段2) in (select 字段1,字段2 form old_table group by 字段1,字段2 having count(*)>1) and id not in (select min(id) from old_table by 字段1,字段2 having count(*)>1);

猜你喜欢

转载自blog.csdn.net/weixin_41671390/article/details/81217727