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

相关文章

QT移植到RK3568开发板的方法步骤

《QT移植到RK3568开发板的方法步骤》本文主要介绍了QT移植到RK3568开发板的方法步骤,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录前言一、获取SDK1. 安装依赖2. 获取SDK资源包3. SDK工程目录介绍4. 获取补丁包二

Java反转字符串的五种方法总结

《Java反转字符串的五种方法总结》:本文主要介绍五种在Java中反转字符串的方法,包括使用StringBuilder的reverse()方法、字符数组、自定义StringBuilder方法、直接... 目录前言方法一:使用StringBuilder的reverse()方法方法二:使用字符数组方法三:使用自

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固 通俗易懂版)

《MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固通俗易懂版)》本文主要讲解了MySQL中的多表查询,包括子查询、笛卡尔积、自连接、多表查询的实现方法以及多列子查询等,通过实际例子和操... 目录复合查询1. 回顾查询基本操作group by 分组having1. 显示部门号为10的部门名,员

java导出pdf文件的详细实现方法

《java导出pdf文件的详细实现方法》:本文主要介绍java导出pdf文件的详细实现方法,包括制作模板、获取中文字体文件、实现后端服务以及前端发起请求并生成下载链接,需要的朋友可以参考下... 目录使用注意点包含内容1、制作pdf模板2、获取pdf导出中文需要的文件3、实现4、前端发起请求并生成下载链接使

MyBatis-Plus中静态工具Db的多种用法及实例分析

《MyBatis-Plus中静态工具Db的多种用法及实例分析》本文将详细讲解MyBatis-Plus中静态工具Db的各种用法,并结合具体案例进行演示和说明,具有很好的参考价值,希望对大家有所帮助,如有... 目录MyBATis-Plus中静态工具Db的多种用法及实例案例背景使用静态工具Db进行数据库操作插入

MySQL中COALESCE函数示例详解

《MySQL中COALESCE函数示例详解》COALESCE是一个功能强大且常用的SQL函数,主要用来处理NULL值和实现灵活的值选择策略,能够使查询逻辑更清晰、简洁,:本文主要介绍MySQL中C... 目录语法示例1. 替换 NULL 值2. 用于字段默认值3. 多列优先级4. 结合聚合函数注意事项总结C

通过ibd文件恢复MySql数据的操作方法

《通过ibd文件恢复MySql数据的操作方法》文章介绍通过.ibd文件恢复MySQL数据的过程,包括知道表结构和不知道表结构两种情况,对于知道表结构的情况,可以直接将.ibd文件复制到新的数据库目录并... 目录第一种情况:知道表结构第二种情况:不知道表结构总结今天干了一件大事,安装1Panel导致原来服务

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D