oracle 吗?
if (is)emp 表中删除重复的empno delete from emp a 
where exists(select b.empno from emp b where a.empno=b.empno 
group by b.empno having count(*)>1) 
and rowid not in (select min(rowid) from emp c where c.empno=a.empno); 

解决方案 »

  1.   

    保留了重复记录中 rowid 最小的记录
      

  2.   

    第一在sql server
    delete a from tablename a,
    (select 重复的字段 as i_重复的字段,max(primary key) as iid  from tablename group by 重复的字段 having count(*)>1) as b
    where a.重复的字段=b.i_重复的字段 and a.primary key<iid
    这是在sql server里面的处理!
    第二在oracle下
    方法原理: 
    1、Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,
      rowid确定了每条记录是在ORACLE中的哪一个数据文件、块、行上。2、在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中
      那些具有最大rowid的就可以了,其余全部删除。3、以下语句用到了3项技巧:rowid、子查询、别名。实现方法: 
    SQL> create table a (
      2  bm char(4), --编码
      3  mc varchar2(20) --名称
      4  )
      5  /表已建立.SQL> insert into a values('1111','1111');
    SQL> insert into a values('1112','1111');
    SQL> insert into a values('1113','1111');
    SQL> insert into a values('1114','1111');SQL> insert into a select * from a;插入4个记录.SQL> commit;完全提交.SQL> select rowid,bm,mc from a;ROWID              BM   MC
    ------------------ ---- -------
    000000D5.0000.0002 1111 1111
    000000D5.0001.0002 1112 1111
    000000D5.0002.0002 1113 1111
    000000D5.0003.0002 1114 1111
    000000D5.0004.0002 1111 1111
    000000D5.0005.0002 1112 1111
    000000D5.0006.0002 1113 1111
    000000D5.0007.0002 1114 1111查询到8记录.查出重复记录
    SQL> select rowid,bm,mc from a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc);ROWID              BM   MC
    ------------------ ---- --------------------
    000000D5.0000.0002 1111 1111
    000000D5.0001.0002 1112 1111
    000000D5.0002.0002 1113 1111
    000000D5.0003.0002 1114 1111删除重复记录
    SQL> delete from a a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc);删除4个记录.SQL> select rowid,bm,mc from a;ROWID              BM   MC
    ------------------ ---- --------------------
    000000D5.0004.0002 1111 1111
    000000D5.0005.0002 1112 1111
    000000D5.0006.0002 1113 1111
    000000D5.0007.0002 1114 1111
    两个操作都是保存最大的数据
      

  3.   

    oracle 里面有 rowid 。
    那么sql server 只能自己定义一个自增长的ID 来完成了,方法同上。emp 表中删除重复的 a ,b ,c 字段。
    delete from emp where not ( emp.ID  in (select min(ID) as minID from emp group by a,b,c ) )
      

  4.   

    我在sybase中用:
    select phone from user a where exists(select a.phone from user b where a.phone=b.phone group by b.phone having count(*)>1) 
    and id not in (select min(id) from user c where c.phone=a.phone);
    其中id为自增长字段.但出现以下错: 
    Adaptive Server finds no legal query plan for this statement. If an Abstract
    Plan is forcing the query plan, check its correspondence to the query. If not,
    please contact Sybase Technical Support.
      

  5.   

    这个问题无论在那里都不会象拟稿的那样!在sybase里面和sql server里面一样的!你可以式样
    delete a from tablename a,
    (select 重复的字段 as i_重复的字段,max(primary key) as iid  from tablename group by 重复的字段 having count(*)>1) as b
    where a.重复的字段=b.i_重复的字段 and a.primary key<iid
    你把这里的primary key改为你的id字段就可以了!
    这句话一定要错
    select a.phone from user b where a.phone=b.phone group by b.phone having count(*)>1!我也没有什么意思
    你那句话改为
    select a.phone from user a 
    where  a.id not in (select min(b.id) from user b where b.phone=a.phone);
     
      

  6.   

    select a.phone from user a 
    where  a.id not in (select min(b.id) from user b where b.phone=a.phone);
    这一句也不行阿!
    还是一样的出错信息!
      

  7.   

    方法都给你了!我这里也成功了!我的具体离子
    CREATE TABLE w (
    iid float,
    k float);
    INSERT INTO w VALUES (
    1,
    2);
    INSERT INTO w VALUES (
    2,
    2);
    INSERT INTO w VALUES (
    3,
    2);
    INSERT INTO w VALUES (
    4,
    2);
    select K  
    from w 
    where iid not in(select min(b.iid) from w  b where b.k=w.k) 
    这是在sql anywhere里面的离子!看看
      

  8.   

    先查找,找到一个删除一个啦,select <> for ....
       dele
       pack
      

  9.   

    CREATE 一游标,查一个,删一个