Record duplicate processing scheme in MySQL table

Source: Internet
Author: User


Today I wrote an airport table with a few fields

Primary key ID, airport English name, airport Chinese name, airport three codewords, Airport city name
ID, name, c_name, Code,city_name
Since the airport three codewords are not duplicated, I initially added a unique index to the airport table


ALTER TABLE ' airport ' ADD UNIQUE (' Code ');

But in the process of writing found that I crawled to the information there is duplication, so temporarily remove the unique index.


Show index from airport;//View index
DROP INDEX code on Airport
Up and down Reptile program code

<?php
Require (' phpquery.php ');
Phpquery::newdocumentfile ("http://****************** for some reason ***********");
$res = PQ (' tbody ')->find (' tr ')->text ();

INSERT into a table according to your business logic

Soon finished the work, after completion, still want to understand the three codewords repeat problem.

My approach was to take all the smallest three codewords records for deletion, with the earliest written code as follows:

DELETE from Airport WHERE
ID in (SELECT ID from Airport GROUP by code has COUNT (code) > 1)
and ID not in (SELECT Max (ID) from airport GROUP by code has COUNT (code) >1);

However, there was an error running this SQL because the deletion or update operation could not be synchronized at the time of the selection. It's time to refer to a temporary table.


Create temporary table TMP Select ID from Airport WHERE
ID in (SELECT ID from Airport GROUP by code has COUNT (code) > 1)
and ID not in (SELECT Max (ID) from airport GROUP by code has COUNT (code) >1);

Delete from airport where ID in (SELECT ID from TMP)
OK, the operation is complete, and then see if there is any duplication:


SELECT ID from Airport GROUP by code has COUNT (code) > 1

If there are any further deletions. The three codewords of the table have not been repeated, then add a unique index to the three codewords ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.