zoukankan      html  css  js  c++  java
  • rowid去重(删除表的重复记录)

    -- 构造测试环境
    SQL> create table andy(id int,name varchar2(10));
    Table created.
    SQL>
    insert into andy values(1,'a');
    insert into andy values(2,'b');
    insert into andy values(3,'c');
    insert into andy values(4,'d');
    SQL> select * from andy;

    ID NAME
    ---------- ----------
    1 a
    2 b
    3 c
    4 d
    4 rows selected.
    SQL>
    insert into andy values(4,'f');
    insert into andy values(4,'d');

    SQL> select * from andy;
    ID NAME
    ---------- ----------
    1 a
    2 b
    3 c
    4 d
    4 f
    4 d
    6 rows selected.
    -- 依次group by 表所有字段,通过min(rowid)查看所有唯一记录(去重记录,也就是相同多行数据只显示一行)
    SQL> select id,name,min(rowid)
    from andy
    group by id,name;
    ID NAME MIN(ROWID)
    ---------- ---------- ------------------
    3 c AAAfKTAAEAAACr/AAC
    4 d AAAfKTAAEAAACr/AAD
    4 f AAAfKTAAEAAACr/AAJ
    1 a AAAfKTAAEAAACr/AAA
    2 b AAAfKTAAEAAACr/AAB
    -- delete 重复数据时,group by 表的个别字段,发现误删除
    SQL> delete from andy
    where rowid not in (
    select min(rowid)
    from andy
    group by id);
    2 rows deleted.
    说明:记录 4 f 被误删。
    SQL> select * from andy;

    ID NAME
    ---------- ----------
    1 a
    2 b
    3 c
    4 d
    -- 构造与上面测试相同环境,即插入刚删除的数据
    SQL>
    insert into andy values(4,'f');
    insert into andy values(4,'d');
    SQL> select * from andy;
    ID NAME
    ---------- ----------
    1 a
    2 b
    3 c
    4 d
    4 f
    4 d
    6 rows selected.
    -- 依次group by 表所有字段,通过min(rowid)查看所有唯一记录(去重记录,也就是相同多行数据只显示一行)
    SQL> select id,name,min(rowid)
    from andy
    group by id,name;
    ID NAME MIN(ROWID)
    ---------- ---------- ------------------
    3 c AAAfKTAAEAAACr/AAC
    4 d AAAfKTAAEAAACr/AAD
    4 f AAAfKTAAEAAACr/AAL
    1 a AAAfKTAAEAAACr/AAA
    2 b AAAfKTAAEAAACr/AAB
    -- delete 重复数据时,group by 表的所有字段,发现没有误删。
    SQL> delete from andy
    where rowid not in (
    select min(rowid)
    from andy
    group by id,name);
    1 row deleted.
    -- 检查去重后的数据,发现没有误删
    SQL> select * from andy;
    ID NAME
    ---------- ----------
    1 a
    2 b
    3 c
    4 d
    4 f
    说明:
    如果想通过rowid去重,那么在 delete 重复数据时,需要group by 表的所有字段。如果只group by 表的个别字段,那么会造成误删除。

  • 相关阅读:
    jboss hello world
    jboss 7 启动问题
    jboss [how to access the admin console]
    tomee 消息持久化
    Python、C和Java对比
    编程语言产生时间表
    从关系型数据库到非关系型数据库
    约束和异常处理
    类与类之间的关系
    类的成员
  • 原文地址:https://www.cnblogs.com/andy6/p/6900926.html
Copyright © 2011-2022 走看看