简体   繁体   English

Mysql删除重复项

[英]Mysql delete duplicates

I'm able to display duplicates in my table 我能够在表格中显示重复项

table name reportingdetail and column name ReportingDetailID 表名reportdetail和列名ReportingDetailID

SELECT DISTINCT ReportingDetailID from reportingdetail group by ReportingDetailID  HAVING count(ReportingDetailID) > 1;
+-------------------+
| ReportingDetailID |
+-------------------+
|         664602311 | 
+-------------------+
1 row in set (2.81 sec)

Dose anyone know how can I go about deleting duplicates and keep only one record? 任何人都知道如何删除重复项并保留一条记录?

I tired the following 我厌倦了以下

SELECT * FROM reportingdetail USING reportingdetail, reportingdetail AS vtable  WHERE      (reportingdetailID > vtable.id)  AND (reportingdetail.reportingdetailID=reportingdetailID);

But it just deleted everything and kept single duplicates records! 但它只删除了所有内容并保留了单个重复记录!

The quickest way (that I know of) to remove duplicates in MySQL is by adding an index. 在MySQL中删除重复项的最快方法(我知道)是添加一个索引。

Eg, assuming reportingdetailID is going to be the PK for that table: 例如,假设reportingdetailID将成为该表的PK:

mysql> ALTER IGNORE TABLE reportingdetail 
    -> ADD PRIMARY KEY (reportingdetailID);

From the documentation : 文档

IGNORE is a MySQL extension to standard SQL. IGNORE是标准SQL的MySQL扩展。 It controls how ALTER TABLE works if there are duplicates on unique keys in the new table or if warnings occur when strict mode is enabled. 如果新表中的唯一键上存在重复项,或者启用了严格模式时出现警告,它将控制ALTER TABLE的工作方式。 If IGNORE is not specified, the copy is aborted and rolled back if duplicate-key errors occur. 如果未指定IGNORE,则复制将中止并在发生重复键错误时回滚。 If IGNORE is specified, only the first row is used of rows with duplicates on a unique key. 如果指定了IGNORE,则只使用第一行在唯一键上具有重复项的行。 The other conflicting rows are deleted. 其他冲突的行将被删除。 Incorrect values are truncated to the closest matching acceptable value. 不正确的值将截断为最接近的匹配可接受值。

Adding this index will both remove duplicates and prevent any future duplicates from being inserted. 添加此索引将删除重复项并防止将来插入任何重复项。 If you do not want the latter behavior, just drop the index after creating it. 如果您不想要后一种行为,只需在创建索引后删除它。

The following MySQL commands will create a temporary table and populate it with all columns GROUPED by one column name (the column that has duplicates) and order them by the primary key ascending. 以下MySQL命令将创建一个临时表,并使用一个列名称(具有重复项的列)将所有列GROUPED填充它,并按主键升序对它们进行排序。 The second command creates a real table from the temporary table. 第二个命令从临时表创建一个真实的表。 The third command drops the table that is being used and finally the last command renames the second temporary table to the current being used table name. 第三个命令删除正在使用的表,最后一个命令将第二个临时表重命名为当前正在使用的表名。

Thats a really fast solution. 这是一个非常快速的解决方案。 Here are the four commands: 以下是四个命令:

CREATE TEMPORARY TABLE videos_temp AS SELECT * FROM videos GROUP by
    title ORDER BY videoid ASC;
CREATE TABLE videos_temp2 AS SELECT * FROM videos_temp;
DROP TABLE videos;
ALTER TABLE videos_temp2 RENAME videos;

这应该给你重复的条目。

SELECT `ReportingDetailID`, COUNT(`ReportingDetailID`) AS Nummber_of_Occurrences FROM reportingdetail GROUP BY `ReportingDetailID` HAVING ( COUNT(`ReportingDetailID`) > 1 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM