2

Want to find duplicate records in a MySQL Database. This could be written with:

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

Which gives me:

101 Saint ST    3

I want to pull it so that it shows each row that is duplicate. example like:

SIM    JANE    101 Saint ST
JOHN   SMITH   101 Saint ST

Anyone have any idea how it could be done? I'm trying to avoid doing the first one then looking up the duplicates with a second query in the code.

Mac23
  • 62
  • 4

1 Answers1

0

One option here is to join your original table to a subquery which determines the counts:

SELECT t1.last, t1.first, t1.address
FROM list t1
INNER JOIN
(
    SELECT address
    FROM list
    GROUP BY address
    HAVING COUNT(*) > 1
) t2
    ON t1.address = t2.address

The subquery servers a filter which removes records not having at least one other record with a common address. Note that I removed the COUNT from the select list of this subquery, since you don't appear to be using it in your final result.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360