Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find duplicate records in MySQL

I want to pull out duplicate records in a MySQL Database. This can be done with:

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

Which results in:

100 MAIN ST    2 

I would like to pull it so that it shows each row that is a duplicate. Something like:

JIM    JONES    100 MAIN ST JOHN   SMITH    100 MAIN ST 

Any thoughts on how this can be done? I'm trying to avoid doing the first one then looking up the duplicates with a second query in the code.

like image 608
Chris Bartow Avatar asked May 12 '09 18:05

Chris Bartow


People also ask

How do I find duplicate entries in MySQL?

First, use the GROUP BY clause to group all rows by the target column, which is the column that you want to check duplicate. Then, use the COUNT() function in the HAVING clause to check if any group have more than 1 element. These groups are duplicate.

Can MySQL have duplicate rows?

MySQL is a database application that stores data in tables in the form of rows and columns. This database application can store duplicate records in the table, which can impact the performance of the database in MySQL.

How do you find duplicate record in a table?

One way to find duplicate records from the table is the GROUP BY statement. The GROUP BY statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has the same values in different rows then it will arrange these rows in a group.


2 Answers

SELECT date FROM logs group by date having count(*) >= 2 
like image 29
trt Avatar answered Sep 28 '22 21:09

trt


The key is to rewrite this query so that it can be used 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; 
like image 120
Powerlord Avatar answered Sep 28 '22 23:09

Powerlord