1. 程式人生 > >幾個刪除重複記錄的sql語句

幾個刪除重複記錄的sql語句

(1)用rowid方法據據oracle帶的rowid屬性,進行判斷,是否存在重複,語句如下:
查資料:
select * from table1 a where rowid !=(select max(rowid)
from table1 b where a.name1=b.name1 and a.name2=b.name2......)
刪資料:
delete from table1 a where rowid !=(select max(rowid)
from table1 b where a.name1=b.name1 and a.name2=b.name2......)
(2)group by方法
查資料:
  select count(num), max(name) from student --列出重複的記錄數,並列出他的name屬性
  group by num
  having count(num) >1 --按num分組後找出表中num列重複,即出現次數大於一次
刪資料:
  delete from student
  group by num
  having count(num) >1
  這樣的話就把所有重複的都刪除了。
(3)用distinct方法 -對於小的表比較有用
create table table_new as select distinct * from table1 minux
truncate table table1;
i