[摘]在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中的乐观锁与悲观锁

《数据库面试必备之MySQL中的乐观锁与悲观锁》:本文主要介绍数据库面试必备之MySQL中乐观锁与悲观锁的相关资料,乐观锁适用于读多写少的场景,通过版本号检查避免冲突,而悲观锁适用于写多读少且对数... 目录一、引言二、乐观锁(一)原理(二)应用场景(三)示例代码三、悲观锁(一)原理(二)应用场景(三)示例

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

电脑死机无反应怎么强制重启? 一文读懂方法及注意事项

《电脑死机无反应怎么强制重启?一文读懂方法及注意事项》在日常使用电脑的过程中,我们难免会遇到电脑无法正常启动的情况,本文将详细介绍几种常见的电脑强制开机方法,并探讨在强制开机后应注意的事项,以及如何... 在日常生活和工作中,我们经常会遇到电脑突然无反应的情况,这时候强制重启就成了解决问题的“救命稻草”。那

SQL表间关联查询实例详解

《SQL表间关联查询实例详解》本文主要讲解SQL语句中常用的表间关联查询方式,包括:左连接(leftjoin)、右连接(rightjoin)、全连接(fulljoin)、内连接(innerjoin)、... 目录简介样例准备左外连接右外连接全外连接内连接交叉连接自然连接简介本文主要讲解SQL语句中常用的表

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

SQL server配置管理器找不到如何打开它

《SQLserver配置管理器找不到如何打开它》最近遇到了SQLserver配置管理器打不开的问题,尝试在开始菜单栏搜SQLServerManager无果,于是将自己找到的方法总结分享给大家,对SQ... 目录方法一:桌面图标进入方法二:运行窗口进入方法三:查找文件路径方法四:检查 SQL Server 安

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@