简体   繁体   中英

Mysql delete duplicates

I'm able to display duplicates in my table

table name reportingdetail and column name 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.

Eg, assuming reportingdetailID is going to be the PK for that table:

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

From the documentation :

IGNORE is a MySQL extension to standard SQL. 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. If IGNORE is not specified, the copy is aborted and rolled back if duplicate-key errors occur. If IGNORE is specified, only the first row is used of rows with duplicates on a unique key. 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. 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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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