20

Possible Duplicate:
Find duplicate records in MySQL

I have a table in MySQL like this:

ID    name    email
1    john     abc@abc.com
2    johnny   abc@abc.com
3    jim      eee@eee.com
4    Michael  abec@awwbc.com

How can I have the MySQL query that will list out the duplicate one like this?

Result of duplicate search:

ID    name    email         Duplicate
1    john     abc@abc.com      2
2    johnny   abc@abc.com      2
Community
  • 1
  • 1
user1995781
  • 19,085
  • 45
  • 135
  • 236

3 Answers3

35
SELECT  a.*, b.totalCount AS Duplicate
FROM    tablename a
        INNER JOIN
        (
            SELECT  email, COUNT(*) totalCount
            FROM    tableName
            GROUP   BY email
        ) b ON a.email = b.email
WHERE   b.totalCount >= 2

for better performance, add an INDEX on column EMail.

OR

SELECT  a.*, b.totalCount AS Duplicate
FROM    tablename a
        INNER JOIN
        (
            SELECT  email, COUNT(*) totalCount
            FROM    tableName
            GROUP   BY email
            HAVING  COUNT(*) >= 2
        ) b ON a.email = b.email
John Woo
  • 258,903
  • 69
  • 498
  • 492
3

If you can live with having the ID and name in comma separated lists, then you can try:

select email, count(*) as numdups,
       group_concat(id order by id), group_concat(name order by id)
from t
group by email
having count(*) > 1

This saves a join, although the result is not in a relational format.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Check this post on the MySQL forums, which gives the following:

SELECT t1.id, t1.name, t1.email FROM t1 INNER JOIN ( 
SELECT colA,colB,COUNT(*) FROM t1 GROUP BY colA,colB HAVING COUNT(*)>1) as t2 
ON t1.email = t2.email;
hd1
  • 33,938
  • 5
  • 80
  • 91