简体   繁体   中英

Remove duplicates records in a MySQL table with key but not primary key

I need to remove duplicate records (just to keep one copy) from a MySQL table in MyISAM format. I have read many questions in Stackoverflow about similar issues but always the people use an ID field as unique id or similar and I haven't this field because my "Key" could be duplicated. I want to remove rows with the 'Fecha' and 'Equip' duplicated.

CREATE TABLE `pfc` (
`Fecha` datetime NOT NULL,
`Equip` int(10) unsigned NOT NULL,
`Value` double NOT NULL,
KEY `Fecha` (`Fecha`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AVG_ROW_LENGTH=21 ROW_FORMAT=FIXED;

An example table data:

Fecha              | Equip | Value 
06/02/2011 0:00:11 |  22   |  29.0
06/02/2011 0:22:11 |  22   |  45.3
06/02/2011 0:00:11 |  22   |  29.0

The result should be:

Fecha              | Equip | Value 
06/02/2011 0:00:11 |  22   |  29.0
06/02/2011 0:22:11 |  22   |  45.3

This structure is not possible to change. I cannot use PRIMARY KEY or UNIQUE INDEX solutions. To create a temporal table without duplicates and then rename would be a poor solutions also, because the KEY and another parameters will be lost.

Thanks

ALTER IGNORE TABLE `pfc` ADD UNIQUE(`Fetcha`, `Equip`);

That will keep the first record it finds and remove duplicates from your table.

From MySQL manual:

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.

Edit:

To create an exact copy of your table, use the following:

CREATE TABLE table_copy SELECT * FROM pfc;

That will copy the table structure and the data. After that, run the ALTER command to add the UNIQUE constraint and filter out the records that are duplicated.

DELETE n1 FROM pfc n1, pfc n2 WHERE n1.Fecha = n2.Fecha AND n1.Equip=n2.Equip AND n1.Value=n2.Value

Just as a test, try this to see if it is what you want

CREATE TABLE new_pfc AS
SELECT * FROM pfc GROUP BY fecha, equip

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