前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在MySQL中查找重复记录

在MySQL中查找重复记录

作者头像
kirin
发布2021-04-30 14:30:34
3.8K0
发布2021-04-30 14:30:34
举报
文章被收录于专栏:Kirin博客Kirin博客

本文翻译自:Find duplicate records in MySQL

I want to pull out duplicate records in a MySQL Database. 我想在MySQL数据库中提取重复记录。 This can be done with: 这可以通过以下方式完成:

代码语言:javascript
复制
  1. SELECT address, count(id) as cnt FROM list
  2. GROUP BY address HAVING cnt > 1
代码语言:javascript
复制

Which results in: 结果是:

代码语言:javascript
复制
100 MAIN ST    2

I would like to pull it so that it shows each row that is a duplicate. 我想将其拉出,以使其显示重复的每一行。 Something like: 就像是:

代码语言:javascript
复制
  1. JIM JONES 100 MAIN ST
  2. JOHN SMITH 100 MAIN ST
代码语言:javascript
复制

Any thoughts on how this can be done? 关于如何做到这一点有什么想法? I’m trying to avoid doing the first one then looking up the duplicates with a second query in the code. 我试图避免做第一个,然后在代码中用第二个查询查找重复项。


#1楼

参考:https://stackoom.com/question/3aCG/在MySQL中查找重复记录


#2楼

Another solution would be to use table aliases, like so: 另一种解决方案是使用表别名,如下所示:

代码语言:javascript
复制
  1. SELECT p1.id, p2.id, p1.address
  2. FROM list AS p1, list AS p2
  3. WHERE p1.address = p2.address
  4. AND p1.id != p2.id
代码语言:javascript
复制

All you’re really doing in this case is taking the original list table, creating two p retend tables — p 1 and p 2 — out of that, and then performing a join on the address column (line 3). 在这种情况下,您真正​​要做的就是获取原始列表表,从中创建两个p后端表-p 1p 2 ,然后在address列上执行联接(第3行)。 The 4th line makes sure that the same record doesn’t show up multiple times in your set of results (“duplicate duplicates”). 第四行确保同一条记录不会在您的结果集中多次出现(“重复重复”)。


#3楼

代码语言:javascript
复制
select * from table_name t1 inner join (select distinct <attribute list> from table_name as temp)t2 where t1.attribute_name = t2.attribute_name

For your table it would be something like 对于您的桌子,它就像

代码语言:javascript
复制
select * from list l1 inner join (select distinct address from list as list2)l2 where l1.address=l2.address

This query will give you all the distinct address entries in your list table… I am not sure how this will work if you have any primary key values for name, etc.. 该查询将为您提供列表中所有不同的地址条目…如果您有名称等任何主键值,我不确定这将如何工作。


#4楼

Fastest duplicates removal queries procedure: 最快的重复项删除查询过程:

代码语言:javascript
复制
  1. /* create temp table with one primary column id */
  2. INSERT INTO temp(id) SELECT MIN(id) FROM list GROUP BY (isbn) HAVING COUNT(*)>1;
  3. DELETE FROM list WHERE id IN (SELECT id FROM temp);
  4. DELETE FROM temp;
代码语言:javascript
复制

#5楼

代码语言:javascript
复制
SELECT t.*,(select count(*) from city as tt where tt.name=t.name) as count FROM `city` as t where (select count(*) from city as tt where tt.name=t.name) > 1 order by count desc

Replace city with your Table. 用表格替换城市 。 Replace name with your field name 您的字段名称替换名称


#6楼

we can found the duplicates depends on more then one fields also.For those cases you can use below format. 我们发现重复项还取决于一个以上的字段。对于这些情况,您可以使用以下格式。

代码语言:javascript
复制
  1. SELECT COUNT(*), column1, column2
  2. FROM tablename
  3. GROUP BY column1, column2
  4. HAVING COUNT(*)>1
代码语言:javascript
复制
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-04-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • #1楼
  • #2楼
  • #3楼
  • #4楼
  • #5楼
  • #6楼
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档