zoukankan      html  css  js  c++  java
  • Oracle中查询和删除相同记录的3种方法

    --创建测试表
    create table test(id number, name varchar2(20));
    insert into test(id, name) values(1,'10');
    insert into test(id, name) values(1,'10');
    insert into test(id, name) values(2,'20');
    insert into test(id, name) values(2,'30');
    insert into test(id, name) values(3,'40');
    insert into test(id, name) values(3,'40');
    commit;
    select * from test;
    
    --查询相同记录
    select id,name from(select id,name,count(*) from test group by id,name having count(*)>1);
    select id,name from test a where rowid > (select min(rowid) from test b where a.id = b.id and a.name = b.name);
    select id,name from test a where rowid <> (select max(rowid) from test b where a.id = b.id and a.name = b.name);
    
    --查询不同记录
    1.select * from test t where t.rowid <= (select min(x.rowid) from test x where t.id = x.id and t.name = x.name);
    2.select distinct t.* from test t;
    
    --删除重复记录
    1.delete from test t where t.rowid > (select min(x.rowid) from test x where t.id = x.id and t.name = x.name);
    2.delete from test t where t.rowid <> (select min(x.rowid) from test x where t.id = x.id and t.name = x.name);
    3.delete from test 
            where rowid in(select rd 
                           from(select rowid rd,row_number()over(partition by id,name order by 1 )rn 
                                  from test
                               ) 
                         where rn <>1
                         );
  • 相关阅读:
    命令
    碎片知识
    驱动问题
    网络基础知识普及
    面向设计大作业——公司餐厅
    面向对象--购物车
    OO之接口-DAO模式代码阅读及应用
    有理数类的定义
    图知识点总结
    Java课程设计总结
  • 原文地址:https://www.cnblogs.com/huangbiquan/p/8232977.html
Copyright © 2011-2022 走看看