六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 1801|回复: 0

删除重复记录

[复制链接]

升级  17.33%

74

主题

74

主题

74

主题

举人

Rank: 3Rank: 3

积分
252
 楼主| 发表于 2013-1-30 02:11:35 | 显示全部楼层 |阅读模式
Friday March 24, 2006 - 10:03am (CST)

    昨天写了一句下面的sql:
    delete from tempRelation where BANKRELATIONID not exist (select distinct BANKRELATIONID from tempRelation )
    失败!
    这个写法不光不能执行,还把exist当作in 来理解了。 exist只是表示有返回结果集,只作用于集合;in表示一个变量是否在一组定值中。
    一般情况下,外部表的结果比较大,内部表的结果比较小时,用IN。如果外部表的结果比较小,而内部表的结果比较大时,用EXISTS。
    下面这个是exist和in的转换使用:
    select col1, col2 from xxx where col3 in (select col4 from yyyy)
    可以改写成
    select col1, col2 from xxx exists (select 'X' from yyyy where xxx.col3=yyyy.col4)

    删除重复记录有以下几种方法:

    sample表 employee表结构如下:
    SQL> desc employee

     Name                                      Null?    Type
     ----------------------------------------- -------- ------------------

    emp_id                                                NUMBER(10)
    emp_name                                           VARCHAR2(20)
    salary                                                  NUMBER(10,2)

    1)通过建立临时表来实现

                     SQL>create table temp_emp as (select distinct * from employee)

                     SQL>truncate table employee; (清空employee表的数据)

                     SQL>insert into employee select * from temp_emp;  (再将临时表里的内容插回来)

    2)通过唯一rowid实现删除重复记录.在Oracle,informix中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是在表中的哪一个数据文件、块、行上。在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中那些具有最大或最小rowid的就可以了,其余全部删除。

    SQL>delete from employee e2 where rowid not in (
            select max(e1.rowid) from employee e1 where         e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--这里用min(rowid)也可以。

     

    3)也是通过rowid,但效率更高。

    SQL>delete from employee where rowid not in (
            select max(t1.rowid) from employee t1 group by

             t1.emp_id,t1.emp_name,t1.salary);--这里用min(rowid)也可以。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表