表中有一列 数据类型是 varchar(64) ,这一列可能重复,现在要把这列重复的行删除,请问怎么能方便 快速的删除呢?如:表
col001       col002    col003
dfag         abc         dgage
fdf          def         xyz
fejif        def         xyz删除后效果
col001       col002    col003
dfag         abc         dgage
fdf          def         xyz

解决方案 »

  1.   

    如:表
    col001       col002    col003
    dfag         abc         dgage
    fdf          def         xyz
    fejif        def         xyz删除后效果
    col001       col002    col003
    dfag         abc         dgage
    fdf          def         xyz以col002,为准.保留任意一行.
    delete from tb where col001 not in (select min(col001) from tb group by col002)
      

  2.   

    楼上,delete from tb where col001 not in (select min(col001) from tb group by col002)后的结果,看不太明白呀感觉应该是 删除后效果为:
    col001       col002    col003
    fdf          def         xyz
      

  3.   

    本人菜鸟:
    create table t(
    col001 varchar(10),
    col002 varchar(10),
    col003 varchar(10))
    insert t select 'dfag','abc','dgage'
    union all select 'fdf','def','xyz'
    union all select 'fejif','def','xyz'
    --select * from t
    delete  from t where col001 not in(
    --重复的话保留最小的
    select min(col001) as col001 from t 
      where  col002 in (--假设col002是有重复的列
        select col002 from t group by col002 having(count(*)>1))

    and col001 not in(
    --不重复的
    select col001 as col001 from t 
      where  col002  not in (
        select col002 from t group by col002 having(count(*)>1)
      )
    )drop table t
    --结果:
    /*
    col001     col002     col003     
    ---------- ---------- ---------- 
    dfag       abc        dgage
    fdf        def        xyz
    */
      

  4.   

    如:表
    col001       col002    col003
    dfag         abc         dgage
    fdf          def         xyz
    fejif        def         xyz如果col002重复的保留一条.
    delete from tb where col001 not in (select min(col001) from tb group by col002)
    如果col002重复的都删除
    delete from tb where col001 in (select col001 from tb group by col002 having count(*) > 1)
      

  5.   

    上面的回答都用到了 in (select ...) 这样当表中数据大于100万时是很慢的,你能想象in 中的数据是100万个以上选项的后果吗?慢死你。
    对于1万条以下记录可以采用,100万以上记录应当采用:
    select b.* from (select col003 from <table> group by col003 ) a left join <table> b on a.col003=b.col003
      

  6.   

    dawugui(潇洒老乌龟) ( ) 信誉:100  2007-09-02 21:51:39  得分: 0  
    ---------
      

  7.   

    兄弟,是删除啊.
    ===================
    既然都能筛选出来了,删除你不会用delete 阿