[摘]在SQL中删除重复记录的多种方法

2023-11-10 21:32

本文主要是介绍[摘]在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 sunshine                                      10000

 

 

 

         1 sunshine                                      10000

 

 

 

         2 semon                                         20000

 

 

 

         2 semon                                         20000

 

 

 

         3 xyz                                           30000

 

 

 

         2 semon                                         20000

 

 

 

 

 

 


SQL>
select distinct * from employee;

 

 

 

    EMP_ID EMP_NAME                                     SALARY

 

 

 

---------- ---------------------------------------- ----------

 

 

 

         1 sunshine                                      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 sunshine                                      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 sunshine                                      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> insert into employee select * from temp_emp;  (再将临时表里的内容插回来)

 

 

 

 

 

 

( 2)通过唯一rowid实现删除重复记录.Oracle中,每一条记录都有一个rowidrowid在整个数据库中是唯一的,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 <(
       
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);

 

 

 

 

 

 

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)也可以。

 

 

 

 

 

 

 

 

 

    EMP_ID EMP_NAME                                     SALARY

 

 

 

---------- ---------------------------------------- ----------

 

 

 

         1 sunshine                                      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 sunshine                                      10000

 

 

 

         1 sunshine                                      10000

 

 

 

         2 semon                                         20000

 

 

 

         2 semon                                         20000

 

 

 

         3 xyz                                           30000

 

 

 

         2 semon                                         20000

 

 

 

 

 

 


SQL>
select distinct * from employee;

 

 

 

    EMP_ID EMP_NAME                                     SALARY

 

 

 

---------- ---------------------------------------- ----------

 

 

 

         1 sunshine                                      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 sunshine                                      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 sunshine                                      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> insert into employee select * from temp_emp;  (再将临时表里的内容插回来)

 

 

 

 

 

 

( 2)通过唯一rowid实现删除重复记录.Oracle中,每一条记录都有一个rowidrowid在整个数据库中是唯一的,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 <(
       
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);

 

 

 

 

 

 

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)也可以。

 

 

 

 

 

 

 

 

 

    EMP_ID EMP_NAME                                     SALARY

 

 

 

---------- ---------------------------------------- ----------

 

 

 

         1 sunshine                                      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 sunshine                                      10000

 

 

 

         1 sunshine                                      10000

 

 

 

         2 semon                                         20000

 

 

 

         2 semon                                         20000

 

 

 

         3 xyz                                           30000

 

 

 

         2 semon                                         20000

 

 

 

 

 

 


SQL>
select distinct * from employee;

 

 

 

    EMP_ID EMP_NAME                                     SALARY

 

 

 

---------- ---------------------------------------- ----------

 

 

 

         1 sunshine                                      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 sunshine                                      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 sunshine                                      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> insert into employee select * from temp_emp;  (再将临时表里的内容插回来)

 

 

 

 

 

 

( 2)通过唯一rowid实现删除重复记录.Oracle中,每一条记录都有一个rowidrowid在整个数据库中是唯一的,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 <(
       
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);

 

 

 

 

 

 

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)也可以。

 

 

 

 

 

 

 

 

 

    EMP_ID EMP_NAME                                     SALARY

 

 

 

---------- ---------------------------------------- ----------

 

 

 

         1 sunshine                                      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 sunshine                                      10000

 

 

 

         1 sunshine                                      10000

 

 

 

         2 semon                                         20000

 

 

 

         2 semon                                         20000

 

 

 

         3 xyz                                           30000

 

 

 

         2 semon                                         20000

 

 

 

 

 

 


SQL>
select distinct * from employee;

 

 

 

    EMP_ID EMP_NAME                                     SALARY

 

 

 

---------- ---------------------------------------- ----------

 

 

 

         1 sunshine                                      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 sunshine                                      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 sunshine                                      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> insert into employee select * from temp_emp;  (再将临时表里的内容插回来)

 

 

 

 

 

 

( 2)通过唯一rowid实现删除重复记录.Oracle中,每一条记录都有一个rowidrowid在整个数据库中是唯一的,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 <(
       
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);

 

 

 

 

 

 

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)也可以。

 

 

 

 

 

 

 

 

 

    EMP_ID EMP_NAME                                     SALARY

 

 

 

---------- ---------------------------------------- ----------

 

 

 

         1 sunshine                                      10000

 

 

 

         3 xyz                                             30000

 

 

 

         2 semon                                         20000

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

这篇关于[摘]在SQL中删除重复记录的多种方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/385393

相关文章

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分