去除表中重复数据(SQL)

有些时候,表中会有重复数据,那么怎么去除重复数据呢?根据查找资料,有以下几种方式可以去除重复数据:

例表的信息如下:

1、创建临时表,完成删除重复数据。

create table test1 as select distinct(id),name,sex from test;

drop table test;

create table test as select * from test1;

drop table test1;

此种方法,简单易懂,但是此种方法不适合数据量特别大的情况。

2、利用oracle数据表所特有的rowid,来删除重复数据。表中的数据即使重复,但表中的行记录rowid也不会重复,rowid记录行数据的物理位置。

delete from test a where rowid<(select max(rowid) from test b where a.id=b.id);

此中的"<"与"max"也可用">"与"min"代替。

3、利用group  by 删除重复数据。

查看重复数据:

 select * from test b
  where b.Id in
        (Select a.Id from test a group by a.Id having count(a.Id) > 1);
 

删除重复数据:

delete from test b
 where b.Id in
       (Select a.Id from test a group by a.Id having count(a.Id) > 1)
and Rowid not in(select min(rowid) from test b group by b.Id having count(b.Id)>1);  

以上是删除重复数据总结,谢谢!