oracle 查询及删除表中重复数据
create table test1(
id number,
name varchar2(20)
);
insert into test1 values(1,'jack');
insert into test1 values(2,'jack');
insert into test1 values(3,'peter');
insert into test1 values(4,'red');
insert into test1 values(5,'green');
insert into test1 values(6,'green');

一 查询表中重复数据
1. 使用exists
select a.* from test1 a where exists (
select name from
( select name ,count(*)
from test1
group by name
having count(*)>1
) b
where a.name = b.name
);

2 join on
select a. * from test1 a
join (
select name ,count(*) from test1
group by name
having count(*)>1
) b
on a.name = b.name;

3 in
select a.name from test1 a
where a.name in
(
select name from test1
group by name
having count(*)>1
);

4 使用rowid 查询得到重复记录里,第一条插入记录后面的记录
select * from test1 a where rowid != (select min(rowid) from test1 b where b.name = a.name);

5 使用rowid查询得到重复记录里,最后一条记录之前插入的记录
select a.* from test1 a where rowid !=(select max(rowid) from test1 b where a.name=b.name);

6 使用rowid 查询得到 不重复的记录和重复记录里最后插入的一条记录
select a.* from test1 a where rowid =(select max(rowid) from test1 b where a.name=b.name);

7 使用rowid 查询得到不重复的记录和重复记录里最先插入的记录
select * from test1 a where rowid = (select min(rowid) from test1 b where b.name = a.name);

删除 所有重复不保留任何一条
delete from test1 a where exists ( select name from (select name ,count(*) from test1 group by name having count(*)>1) b where a.name = b.name);
delete from test1 a where a.name in (select name from test1 group by name having count(*)>1);
删除重复记录里,第一条重复记录后面插入的记录
delete from test1 a where rowid !=(select min(rowid) from test1 b where b.name = a.name);
删除先前插入的重复记录,保留最后插入的重复记录
delete from test1 a where rowid !=(select max(rowid) from test1 b where a.name=b.name);

oracle 查询及删除表中重复数据的更多相关文章
- 查询和删除表中重复数据sql语句
1.查询表中重复数据.select * from peoplewhere peopleId in (select peopleId from people group by ...
- mysql 查询及 删除表中重复数据
CREATE TABLE `test` ( `id` INT(20) NOT NULL AUTO_INCREMENT, `name` VARCHAR(20) NULL DEFAULT NULL, `a ...
- ROWID面试题-删除表中重复数据(重复数据保留一个)
/* ROWID是行ID,通过它一定可以定位到r任意一行的数据记录 ROWID DNAME DEPTNO LOC ------------------ ------------------------ ...
- sqlite 删除表中重复数据(亲测可用)
例子:表名 Paper .通过字段PaperID查找重复数据. 1 --查询某表中重复的数据 select * from Paper group by PaperID having co ...
- sql记录去重(SQL查询或者删除表中重复记录)
.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select*from people where peopleIdin (select peopleIdfrom peopl ...
- MySQL 数据库删除表中重复数据
采集数据的时候,由于先期对页面结构的分析不完善,导致采漏了一部分数据.完善代码之后重新运行 Scrapy,又采集了一些重复的数据,搜了下删除重复数据的方法. N.B. 删除数据表的重复数据时,请先备份 ...
- Oracle通过ROWID删除表中重复记录
-- 1 通过ROWID删除T1表里重复的记录 SELECT ROWID,A,B--DELETE FROM T1WHERE ROWID IN ( SELECT RD FROM ( ...
- oracle查询、删除表中相同的数据
delete FROM tablename a WHERE rowid > ( SELECT min(rowid) FROM tablename b WHERE b.id = a.id and ...
- SQL Server中删除表中重复数据
方法一:利用游标,但要注意主字段或标识列 declare @max integer,@id integer open cur_rows fetch cur_rows into @id,@max beg ...
随机推荐
- Vue 父组件主动获取子组件的值,子组件主动获取父组件的值
父组件主动获取子组件的值 1. 在调用子组件的时候定义一个ref-> ref="header"2. 在父组件中通过this.$refs.header.属性,调用子组件的属性, ...
- kali linux之手动漏洞挖掘三(sql注入)
服务器端程序将用户输入作为参数作为查询条件,直接拼写sql语句,并将结果返回给客户端浏览器 如判断登录 select * from users where user='uname' and passw ...
- PHP编码技巧
原则 正确实现功能 执行速度与快 占系统资源少 后期维护方便 编程注意 1.命名很重要 2.适当的使用注释 3.使用一个变量,需要初始化 4.优先使用单引号 $row['id']的效率是$row[id ...
- linux安装数据库
1.登录数据库 mysql -u root -p xxxxxx 2.创建数据库 create database xx; 3.use xx; 4.source xx.sql
- 190310HTML&CSS&JS
一.HTML 1.web server import socket def handle_request(client): buf = client.recv(1024) client.send(by ...
- 前端CSS的基本素养
前端开发的三驾马车——html.css.js,先谈谈CSS CSS 前期:解决布局.特效.兼容问题 中级:网站风格的制定.色调.模块.布局方式.交互方式.逻辑设计等 高级:模块命名.类的命名.文件的组 ...
- 如何让ie8地址栏下拉框里不显示历史记录和收藏夹
打开浏览器,点击IE浏览器上方工具——Internet选项——内容——自动完成的设置——地址栏里面的勾勾去掉,浏览历史记录和收藏夹里面的东西就自动没有了
- struts2学习笔记(一)—— struts2介绍及入门程序
一.struts2概述 1.1 什么是struts2? 在介绍Struts2之前,先来认识一下Struts1.Struts1是最早的基于MVC模式的轻量级web框架,他能够合理的划分代码结构,并包含了 ...
- Android TCP协议的Socket通信
1.介绍 2.使用方法 3.java后台代码 服务器server package com.lucky.servertest; import java.io.BufferedReader; import ...
- Luogu P1801 黑匣子_NOI导刊2010提高(06)
P1801 黑匣子_NOI导刊2010提高(06) 题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个 ...