如何从mysql数据库中删除重复的条目? [英] How to remove duplicate entries from a mysql db?

查看:422
本文介绍了如何从mysql数据库中删除重复的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格有一些ids +标题。我想使标题列是唯一的,但它已经有超过600k的记录,其中一些是重复的(有时是十几次)。



如何删除所有重复,除了一个,所以我可以在标题列之后添加一个UNIQUE键?

解决方案

此命令添加了一个唯一的键,并删除生成错误的所有行(由于唯一的键)。这将删除重复项。

  ALTER IGNORE TABLE表ADD UNIQUE KEY idx1(title); 






编辑:请注意,此命令可能不适用于InnoDB表。有关解决方法,请参阅此帖。 (感谢匿名用户的信息。)


I have a table with some ids + titles. I want to make the title column unique, but it has over 600k records already, some of which are duplicates (sometimes several dozen times over).

How do I remove all duplicates, except one, so I can add a UNIQUE key to the title column after?

解决方案

This command adds a unique key, and drops all rows that generate errors (due to the unique key). This removes duplicates.

ALTER IGNORE TABLE table ADD UNIQUE KEY idx1(title); 


Edit: Note that this command may not work for InnoDB tables for some versions of MySQL. See this post for a workaround. (Thanks to "an anonymous user" for this information.)

这篇关于如何从mysql数据库中删除重复的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆