Home > Database > Mysql Tutorial > How to Find and Retrieve All Duplicate Records in MySQL?

How to Find and Retrieve All Duplicate Records in MySQL?

DDD
Release: 2024-12-20 07:22:13
Original
805 people have browsed it

How to Find and Retrieve All Duplicate Records in MySQL?

How to Identify Duplicate Records in MySQL

The task of identifying duplicate records in a MySQL database is typically addressed using the following query:

SELECT address, count(id) as cnt 
FROM list
GROUP BY address 
HAVING cnt > 1
Copy after login

However, this query provides only a count of the duplicate records, not the actual data for each duplicate row. To retrieve the individual duplicate records, a slightly different approach is necessary.

The key is to rewrite the original query as a subquery:

SELECT firstname, 
   lastname, 
   list.address 
FROM list
   INNER JOIN (SELECT address
               FROM   list
               GROUP  BY address
               HAVING COUNT(id) > 1) dup
           ON list.address = dup.address;
Copy after login

This subquery identifies the duplicate addresses, and the outer query uses that information to join the list table and retrieve all the associated data for each duplicate record.

The above is the detailed content of How to Find and Retrieve All Duplicate Records in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template