SQL数据库指定列里面,把重复的数据删除,只保留唯一个。请问该如何实现!

解决方案 »

  1.   

    分为数据完全重复和某个字段重复两种情况:1.数据完全重复这种情况比较容易解决,使用 select distinct * from tableName 就可以得到无重复记录的结果集。如果该表需要删除重复的记录(重复记录保留1条),可以通过一个临时表过渡一下来删除:insert into tabletemp select distinct field from table1drop table table1insert into table1 select * from tabletempdrop table tabletemp或者select distinct * into #Tmp from tableNamedrop table tableNameselect * into tableName from #Tmpdrop table #Tmp发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。2.某个字段重复这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下:delete from tableName where id not in (select min(id) from tableName group by name)----只保留重复记录的第一条(id最小的一条)或delete from tableName where field in (select field from tableName group by field having count(*) > 1)或delete from tableName where id in (select max(id) from tableName group by name having count(*)>1)----删除重复记录中ID最大的一条(如果有2条以上的重复记录则需多次执行)上面的方法在删除小数量级的数据时还有用,当一旦处理的数据是几十万或者更多时就出问题了,一般的机器估计一运行就马上给费了。其实稍有点常识的算一算就知道这样的语句会有多大的运算量了,它的运算量至少是以乘方的形式递增的,想想就恐怖。或者假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集select identity(int,1,1) as autoID, * into #Tmp from tableNameselect min(autoID) as autoID into #Tmp2 from #Tmp group by Name,Addressdrop table tableNameselect * into tableName from #Tmp where autoID in(select autoID from #Tmp2)drop table #Tmpdrop tabel #Tmp2最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)。或者insert into tblTemp select * from tableNamedelete from tableName as a where a.id > (select min(b.id) from tblTemp as b where b.field=a.field)drop table tblTemp我在这里主要是要给出对于大数量级的表的重复数据删除的解决方案,其实也很简单,也是利用了一个过渡表来实现。这样利用了数据库的索引的优势,大大的减少运算量。一般就是distinct , group by , #tempTable,当然借助index会更快些。  =======================附加说明====================================----执行动态SQL语句(带参数的存储过程)CREATE PROCEDURE 存储过程名(@num int)ASdeclare @string nvarchar(100)set @string='SELECT TOP '+ CAST (@num as nvarchar) +' * FROM 表名' exec (@string)-----name相同时id小的出现查询语句select ID,NAME from house1 where name='中凯' and roomtype='双人间' and startdate>='2007-5-25' and id in(select min(id) from house1 group by name) 
      

  2.   

    你好,kye_jufei,我照你上面的方法,好像没成功。
    我的数据库有一个表T,分成四个字段,A、B、C、D。现在我想把A设置成主键(以前没有主键的),但是A字段里有数据重复。我现在是想如果A里面有重复的数值,就删除多余,只保留一个。然后才能把A字段设置成这个表的主键。不知道该怎么处理。数据库里面大约有8万条数据吧。!
      

  3.   

    大哥你再試試以下的方法:--<一>:select distinct * into #temp from 表truncate table 表insert 表 select * from #tempdrop table--<二>带有标识列--备份数据select * into #temp from 表alter table #temp drop column id--删除原表数据truncate table msgtable--恢复数据并去掉重复数据insert into 表 select distinct * from #temp--<三>delete From aa where a in ( select a From aa group by a having count(a)>1)--<四>alter table 表 add newfield int identity(1,1)delete 表where newfield not in(select min(newfield) from 表 group by 除newfield外的所有字段)alter table 表 drop column newfield--<五>--添加一个处理的标识字段alter table 表 add id int identity(1,1)go    --删除重复记录delete afrom 表 a left join(select id=min(id) from 表 group by a,b)b on a.id=b.idwhere b.id is nullgo--删除处理用的标识字段alter table 表 drop column id--参考:/*一张表里面以两个字段为唯一字段,当几条记录的这两个字段完全相同时,需要删除重复项,如下表     a   b   c   d     1   2   3   4     1   5   3   5     1   2   7   9     以a、b为唯一字段,第一条和第三条的a、b完全相同,所以,需要删除第一条记录1   2   3   4   或者第三条记录1   2   7   9     即如下结果:     a   b   c   d     1   2   3   4     1   5   3   5     或     a   b   c   d     1   5   3   5     1   2   7   9         请问各位大侠这种sql语句怎么写 */       CREATE TABLE Tb1(id int, [a] varchar(255), [b] varchar(255), [c] varchar(255), [d] varchar(255))INSERT Tb1(id, [a], [b], [c], [d])SELECT 1, '1','2','3','4'UNION ALL SELECT 2, '1','5','3','5'UNION ALL SELECT 3, '1','2','7','9'UNION ALL SELECT 4, '1','4','7','6'delete Tb1 where [id] not in (select max([id]) from Tb1 group by a,b )select * from tb1drop table tb1如果要同时删除第一和第三行即如下结果:a b c d1 5 3 5语句如下:delete m from tb tinner join(select a ,bfrom tbgroup by a , bhaving count(*)>1)non m.a = n.a and m.b = n.b或delete * from tb as m,(select a ,bfrom tbgroup by a , bhaving count(*)>1)nwhere m.a = n.a and m.b = n.b--在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢?谢谢!1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录delete from peoplewhere peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)3、查找表中多余的重复记录(多个字段)select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录delete from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
      

  4.   

    再试试这个:delete from person
    where id in
    (select id from
    (  
      select id,age,row_number() over(partition by age order by id) nn from person  
    )  
    where nn>1)
      

  5.   

    删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
    delete from people 
    where peopleId in (select peopleId from people group by peopleId   
    having count(peopleId) > 1)
    and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
      

  6.   

    最有效,也是最慢的方法是双重循环:
    for i=0 to count1 do 
     for j=0 to count2 do 
       。。