Efficient Query method for repeating fields in MySQL large table

Source: Internet
Author: User

MySQL large table repeating fields How do I find it? This is a lot of people have encountered problems, below teach you a MySQL large table repeat field Query method for your reference.
There is a large table in the database, and you need to find a duplicate record ID in the name to compare.
If you are simply looking for a field in the database that does not duplicate name, it is easy to


The code is as follows:
SELECT min (' id '), ' name '
From ' table '
GROUP by ' name ';

However, this does not get an ID value that says there are duplicate fields. (Only one of the smallest ID values was obtained)
It is also easy to query which fields are duplicated
The code is as follows:
SELECT ' name ', Count (' name ') as Count
From ' table '
GROUP by ' name has count (' name ') >1
Order by Count DESC;

But to query the ID value of a duplicate field at once, you must use a subquery, so use the following statement to implement the MySQL large table repeating field query.
The code is as follows:
SELECT ' id ', ' name '
From ' table '
WHERE ' name ' in (
SELECT ' name '
From ' table '
GROUP by ' name has count (' name ') >1
);

But this statement is so inefficient in MySQL that it feels like MySQL doesn't generate a temporary table for subqueries.
Then use the first to create a temporary table
The code is as follows:
CREATE TABLE ' tmptable ' as (
SELECT ' name '
From ' table '
GROUP by ' name has count (' name ') >1
);

Then use a multiple table connection query
The code is as follows:
SELECT A. ' ID ', a. ' Name '
From ' table ' A, ' tmptable ' t
WHERE A. ' Name ' = T. ' Name ';

As a result, the result came out soon.

Repeat with distinct.

The code is as follows:
SELECT distinct a. ' ID ', a. ' Name '
From ' table ' A, ' tmptable ' t
WHERE A. ' Name ' = T. ' Name ';

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.