[摘]在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

相关文章

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin