mysql 数据表中查找、删除重复记录
为了性能考虑,在阅读之前提醒大家,如果有子查询,子查询查询到的数据最好不要超过总数据量的30%。 查询有重复数据的记录
select * from F group by a,b,c,d having count(*)>1 select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp
SQL删除重复数据方法
例如:
id name value
1 a pp
2 a pp
3 b iii
4 b pp
5 b pp
6 c pp
7 c pp
8 c iii
id是主键
要求得到这样的结果
id name value
1 a pp
3 b iii
4 b pp
6 c pp
8 c iii
方法1
delete YourTable where [id] not in ( select max([id]) from YourTable group by (name + value))
方法2
delete a from 表 a left join( select id=min(id) from 表 group by name,value )b on a.id=b.id where b.id is null
查询及删除重复记录的SQL语句 查询及删除重复记录的SQL语句
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
3、查找表中多余的重复记录(多个字段)
select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
(二)
比方说 在A表中存在一个字段“name”, 而且不同记录之间的“name”值有可能会相同, 现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;
Select Name,Count(*) From A Group By Name Having Count(*) > 1 如果还查性别也相同大则如下: Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1
(三)
方法一
declare @max integer,@id integer declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1 open cur_rows fetch cur_rows into @id,@max while @@fetch_status=0 begin select @max = @max -1 set rowcount @max delete from 表名 where 主字段 = @id fetch cur_rows into @id,@max end close cur_rows set rowcount 0
方法二
"重复记录"有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。
1、对于第一种重复,比较容易解决,使用 select distinct * from tableName 就可以得到无重复记录的结果集。 如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除
select distinct * into #Tmp from tableName drop table tableName select * into tableName from #Tmp drop table #Tmp
发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。
2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下 假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集
select identity(int,1,1) as autoID, * into #Tmp from tableName select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID select * from #Tmp where autoID in(select autoID from #tmp2)
最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)
(四) 查询重复
select * from tablename where id in ( select id from tablename group by id having count(id) > 1 )
去重:
-- //////////////////////////////////实现1
DELETE
FROM
tableName
WHERE
id NOT IN (
SELECT
*
FROM
(
SELECT
min(t3.id)
FROM
tableName t3
GROUP BY
column1(即去重的字段)
) t2
) -- //////////////////////////////////实现2
DELETE
FROM
tableName t1
WHERE
t1.id NOT IN (
SELECT
*
FROM
(
SELECT DISTINCT
id
FROM
tableName t3
) t2
)
mysql 数据表中查找、删除重复记录的更多相关文章
- 删除Mysql数据表中多余的重复记录的sql语句
数据表 sniper_tb 中存在主键 id,字段url,现需要在url字段上添加 unique,但由于url存在重复记录,导致添加失败. 如何删除表中多余的url重复记录,仅保持一条? 思路一 将 ...
- mysql 数据表中查找重复记录
select mobile_phone,count(*) as count from lawyer group by mobile_phone having count>1;
- mysql insert插入时实现如果数据表中主键重复则更新,没有重复则插入的四种方法
[CSDN下载] Powerdesigner 设计主键code不能重复等问题 [CSDN博客] Oracle中用一个序列给两个表创建主键自增功能的后果 [CSDN博客] MySQL自增主键删除后重复问 ...
- SqlServer查找表中多余的重复记录
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from ...
- 在SQL Server中快速删除重复记录
在SQL Server中快速删除重复记录 2006-07-17 21:53:15 分类: SQL Server 开发人员的噩梦——删除重复记录 想必每一位开发人员都有过类似的经历,在对数据库进行查询 ...
- (转)MySQL数据表中带LIKE的字符匹配查询
MySQL数据表中带LIKE的字符匹配查询 2014年07月15日09:56 百科369 MySQL数据表中带LIKE的字符匹配查询 LIKE关键字可以匹配字符串是否相等. 如果字段的值与指定的 ...
- 删除表中多余的重复记录(多个字段),只留有rowid最小的记录
假如表Users,其中ID为自增长. ID,Name,Sex 1 张三,男 2 张三,男 3 李四,女 4 李四,女 5 王五,男 --查找出最小行号ID的重复记录 select Name,Sex,C ...
- 向mysql数据表中插入数据失败的原因
1.案例代码: $sql1="insert into content(category,subject,content,username,release_date) values('{$ca ...
- mysql 删除表中多余的重复记录
=============================================== 2019/7/16_第1次修改 ccb_warlock == ...
随机推荐
- 给自定义cell设置分隔线的不同做法
1.给cell添加一个UIView,设置UIView的高度为1,并设置这个UIView的左.下.右约束. 2.不需要给cell添加任何控件,重写cell的- (void)setFrame:(CGRec ...
- namespace for c++
namespace中文意思是命名空间或者叫名字空间,传统的C++只有一个全局的namespace,但是由于现在的程序的规模越来越大,程序的分工越来越细,全局作用域变得越来越拥挤,每个人都可能使用相同的 ...
- threading示例
多线程举例: import time import threading def worker(): print ("hello.Kamil") time.sleep(1)#等待一秒 ...
- matplotlib 基础
plt.figure(2) #创建图表2 plt.figure(1) #创建图表1 ax1=plt.subplot(211) # 在上面 最近的 图表1上 创建子图1 ax2=plt.subplot( ...
- [分类算法] :SVM支持向量机
Support vector machines 支持向量机,简称SVM 分类算法的目的是学会一个分类函数或者分类模型(分类器),能够把数据库中的数据项映射给定类别中的某一个,从而可以预测未知类别. S ...
- 【poj3264】 Balanced Lineup
http://poj.org/problem?id=3264 (题目链接) 题意 给出序列,求区间最大值-最小值 Solution 无修改,询问较多,ST表水一发. ST算法(Sparse Table ...
- 前端rem单位的使用研究
分析网易新闻手机web端,http://3g.163.com/,发现里面大量使用了rem这个单位进行计算大小. 针对rem这个单位有如下解析: px:像素是相对于显示器屏幕分辨率而言的相对长度单位.p ...
- oracle 前滚和回滚
前滚(Rollforward): 在数据库关闭时候,很多已经提交的数据没有写到磁盘上, 数据恢复时,在文件上重演日志内容,把文件恢复到数据库关闭时的状态. 回滚(Rollback): 在数据库关闭时, ...
- Android成长日记日记-Debug调试程序
Debug调试程序: 1. 调试是程序员无法逃避的工作.调试方法有很多种,但归根结底,就是找到印发错误的代码 2. Debug调试可以快速准确的定位到错误问题的位置,以及它的调用关系 3. Debug ...
- Git 服务器搭建
接下来我们将以 Centos 为例搭建 Git 服务器. 1.安装Git $ yum install curl-devel expat-devel gettext-devel openssl-deve ...