oracle在没

 db_user表中数据:
     name age nub
     张三    13  13
     张三    12  12
     张三    12  13
     张三    12  13
     李四    12  12
     李四    12 12
     


查询重复数据(一条)
sql:select * from db_user group by name,age,nub having count(*)>1;
    (需统计条数conut)


查询出的结果
     name age nub
     张三   12  12
     李四   12 13


查询重复记录(所有)


sql:
select * from db_user  a where (a.name,a.age,a.nub) 
 in 
 (select *  from db_user group by name,age,nub having count(*)>1);


查询结果:
     name age nub
     张三   12  13
     张三   12  13
     李四   12  12
     李四   12  12


删除重复记录保留一条


 步骤:
     1.将查询的数据插入一个新的表中;
     2.删除原来的表的数据
     3.将新表的数据再插入原表中
     4,删除新表


 sql:
     1. create table new_table as(
select * from db_user group by name,age,nub having count(*)>1
);
 
     2.  delete from db_user  a where (a.name,a.age,a.nub)  in 
(select *  from db_user group by name,age,nub having count(*)>1 );


 
不能对同一表子查询后进行插入或者删除 要在子查询再嵌套一个查询 让对该表查询成为孙查询; 
 
     3.insert into db_user (select name,age,nub from new_table);
  
    
     4.drop table new_table;




到此完成操作 最后的数据:







posted @ 2015-09-05 20:55  微软小菜鸟  阅读(124)  评论(0编辑  收藏  举报