在大的数据库应用中,经常因为各种原因遇到重复的记录,造成数据的冗余和维护上的不便。1.用rowid方法2.用group by方法3.用distinct方法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;
insert into table1 select * from table_new; 
 

解决方案 »

  1.   

    --try
    insert into A (Field1、Field2) select B.Field1,B.Field2 from
    A,B where A.Field1<>B.Field1
      

  2.   

    可以看看:
    http://community.csdn.net/Expert/topic/4271/4271982.xml?temp=.8837397http://community.csdn.net/Expert/topic/4224/4224385.xml?temp=.6532556
      

  3.   

    谢谢楼上。
    RowId 与 Groupby 哪一种快一点?
      

  4.   

    select *,identity(int,1,1) as sid  into # from B
    go
    delete a from # a,# b where a.field1=b.field1 and a.sid>b.sid
    go
    insert into A(filed1,field2)
    select filed1,field2 from #
    go
    drop table #,B
    go
      

  5.   

    我个人觉得:zlp321002(人生没有理想,那和咸鱼还有什么两样) 的方法最好,因为你的A表中field1是主键,所以没必要那么麻烦,直接两表相连插入。
      

  6.   

    wgsasd311老大的答复我看不懂,#,# a,# b 是什么?不用先Create吗?
      

  7.   

    wgsasd311老大的答复我看不懂,#,# a,# b 是什么?不用先Create吗?----------# 表示一个临时表 # a,# b 分别表示同一个表(#),取了两个不同的别名 (a,b)
    不用create 了,第一个语句就会自动创建此临时表 #
      

  8.   

    select * into #1 from b
    delete #1 where field1 in(select field1 from a)
    insert a select field1,min(field2) field2 from #1 group by field1
      

  9.   

    zlp321002(人生没有理想,那和咸鱼还有什么两样)
    的方法会不会有问题呢?
    比如B的表中有重复数据,但A中没有这些数据,用Zlp的方法会不会把重复数据插入,
    然后因为主键冲突而失败呢?
      

  10.   

    用自强不息的方法:
    请问老大,怎样用sql语句删除字段sid,然后再插入?
    因为表A没有sid字段。
    而且表B字段很多,select 语句会写道手软。
      

  11.   

    select *,identity(int,1,1) as sid  into # from B
    go
    delete a from # a,# b where a.field1=b.field1 and a.sid>b.sid
    go
    alter table # drop column sid
    go
    insert into A
    select *from #
    go
    drop table #,B
    go
      

  12.   

    alter table # drop column sid ---删除字段 sid