文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页技术开发ASP 学院 → sql 删除重复记录

sql 删除重复记录

时间:2010/5/16 0:13:45来源:本站整理作者:陈风我要评论(0)

sql 删除重复记录的方法

学习sql有一段时间了,发现在我建了一个用来测试的表(没有建索引)中出现了许多的重复记录。后来总结了一些删除重复记录的方法,在Oracle中,可以通过唯一rowid实现删除重复记录;还可以建临时表来实现...这个只提到其中的几种简单实用的方法,希望可以和大家分享(以表employee为例)。
SQL> desc employee
Name                   Null?  Type----------------------------------------- -------- ------------------
emp_id                        NUMBER(10)emp_name                      VARCHAR2(20)
salary                         NUMBER(10,2)
可以通过下面的语句查询重复的记录:
SQL> select * from employee;
  EMP_ID EMP_NAME                 SALARY
---------- ---------------------------------------- ----------
     1 su hine                   10000
     1 su hine                   10000
     2 semon                     20000
     2 semon                     20000
     3 xyz                      30000
     2 semon                     20000
SQL> select distinct * from employee;
  EMP_ID EMP_NAME                   SALARY
---------- ---------------------------------------- ----------
     1 su hine                   10000
     2 semon                     20000
     3 xyz                       30000
SQL> select * from employee group by emp_id,emp_name,salary having count (*)>1
  EMP_ID EMP_NAME                   SALARY
---------- ---------------------------------------- ----------
     1 su hine                   10000
     2 semon                     20000
SQL> select * from employee e1
where rowid in (select max(rowid) from employe e2 where e1.emp_id=e2.emp_id and
 e1.emp_name=e2.emp_name and e1.salary=e2.salary);
  EMP_ID EMP_NAME                   SALARY
---------- ---------------------------------------- ----------
     1 su hine                   10000
     3 xyz                       30000
     2 semon                     20000
2. 删除的几种方法:
(1)通过建立临时表来实现
SQL>create table temp_emp as (select distinct * from employee) 
SQL> truncate table employee; (清空employee表的数据)
SQL> i ert into employee select * from temp_em  (再将临时表里的内容插回来)
( 2)通过唯一rowid实现删除重复记录.在Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是在Oracle中的哪一个数据文件、块、行上。在重复的记录中,可能所有列的内容都相同,但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)也可以。
SQL>delete from employee e2 where rowid 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)也可以。
  EMP_ID EMP_NAME                   SALARY
---------- ---------------------------------------- ----------
     1 su hine                   10000
     3 xyz                       30000
     2 semon                     20000
 
SQL> desc employee
Name                   Null?  Type----------------------------------------- -------- ------------------
emp_id                        NUMBER(10)emp_name                      VARCHAR2(20)
salary                         NUMBER(10,2)
可以通过下面的语句查询重复的记录:
SQL> select * from employee;
  EMP_ID EMP_NAME                 SALARY
---------- ---------------------------------------- ----------
     1 su hine                   10000
     1 su hine                   10000
     2 semon                     20000
     2 semon                     20000
     3 xyz                      30000
     2 semon                     20000
SQL> select distinct * from employee;
  EMP_ID EMP_NAME                   SALARY
---------- ---------------------------------------- ----------
     1 su hine                   10000
     2 semon                     20000
     3 xyz                       30000
SQL> select * from employee group by emp_id,emp_name,salary having count (*)>1
  EMP_ID EMP_NAME                   SALARY
---------- ---------------------------------------- ----------
     1 su hine                   10000
     2 semon                     20000
SQL> select * from employee e1
where rowid in (select max(rowid) from employe e2 where e1.emp_id=e2.emp_id and
 e1.emp_name=e2.emp_name and e1.salary=e2.salary);
  EMP_ID EMP_NAME                   SALARY
---------- ---------------------------------------- ----------
     1 su hine                   10000
     3 xyz                       30000
     2 semon                     20000

相关视频

    没有数据

相关阅读 Mac OS X 系统上如何升级 Mysql 数据库?PLSQL Developer怎么导出数据库PLSQL Developer配置Oralce11g连接plsql developer怎么使用 plsql developer使用教程plsql developer怎么连接数据库 plsql developer连接数据库教程SQL2008全部数据导出导入两种方法mysql数据库root密码忘记的修改方法SQL报表错误“未能为SSL/TLS安全通道建立信任关系”解决方法

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 127.0.0.1 需要输入用Microsoft JET Databa ASP木马密码加密的破解方法Asp中文件上传,fso方法大全js报错 抛出例外解决方法-适用于ecshop 调用127.0.0.1 需要输入用户名密码的解决方法

人气排行 教你如何用ASP生成HTML静态网页127.0.0.1 需要输入用户名密码的解决方法编写简单ASP代码测试WEB服务器环境asp用户登录模块实例代码ASP保存远程文件到本地(实用版)js报错 抛出例外解决方法-适用于ecshop 调用如何调试ASP的环境ASP.NET中MVC框架模式方法如何实现分享