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 ';