如果一张表中有重复的编码如何查找出来并删除?

解决方案 »

  1.   

    delete from 表 where 编码 in (select 编码 from 表 group by 编码 having count(*)>=2)
      

  2.   

    delete from 表 where 编码 in (select 编码 from 表 group by 编码 having count(*)>=2)把相同的都给删了~~
      

  3.   

    用一个while循环
    while (exists(select Top 1 编码 from 表 group by 编码 having count(*)>=2))
    Begin
    delete from 表 where 编码 in (select Top 1 编码 from 表 group by 编码 having count(*)>=2)
    End只删除重复的编号重复的重复项:),
      

  4.   

    刪除1筆得話,cjer() 這個就行
      

  5.   

    先distict然后全删除 再把distic的表更新原来的就行了
      

  6.   

    只留下最上面的一条,select Top 1 编码 from 表  group by 编码 having count(*)>=2 order by 编码 desc 在后头加个order by
      

  7.   

    select distinct * into #tmp from tabledelete from tableinsert into table select * from #tmp
      

  8.   

    select distinct * into #tmp from tabledelete from tableinsert into table select * from #tmp
    是不行的,它编号相同,可其他值可能相同,所以还是会有编号相同的
      

  9.   

    上面的几个直接delete方法都会把重复项目全部删除,一个都不剩的。
      

  10.   

    面試時遇到過這樣問題,他要求我用一條SQL語句解決我是用臨時表保存原表的DISTINCT數據,刪除原表的數據,再把臨時表的數據插入原表中然後我被人說了一頓,說我沒有按要求做,到現在我還想不出如何用一條SQL語句搞定
      

  11.   

    cjer() 的方法是不行的,重复的行全部删除
      

  12.   

    楼主的表中编号重复的记录是否内容完全一样(其他字段也一样)?
    如果是:先distinct到另一个表,清空原表,从另一个表把记录插回去就可以了,如果要如楼上说的用一句sql来完成,那我就不懂了。
    如果否:也就是说编号相同但内容不同,那么就奇怪了,能只保留一条而删掉其他吗?其他的真的对业务没用了吗?谁知道要留下哪一条才是有用的?如果一定要这么做,那就用游标逐行扫重复编号的记录,逐条把他们删除掉。
      

  13.   

    根据cjer() 的改下--插入一个标识列
    alter table 表
    add ID int identity(1,1)--删除含重复编码的行,保留ID最上面的
    delete from 表
    where ID in(
    select ID from 表
    where 编码 in
    (select 编码 from 表 group by 编码 having count(*)>=2)
    )
    and ID not in 
    (
    select ID from 表
    where 编码 in
    (select Top 1 编码 from 表 group by 编码 having count(*)>=2)
    )--删除ID列
    alter tabe 表
    drop column ID
      

  14.   

    有没有简单点,一句SQL能搞定的?重复的编码是完全一样的,只是其他列不一样。
      

  15.   

    你的情况是 总提示"键列信息不足或不正确,更新影响到多行吗?如果是可以这样:假设你有2条重复的数据,删除其中一条可以用
    set rowcount 1
    delete where...
    set rowcount 0
    这样2条只会删除一条。set rowcount x的用法可以查查bookonline