MySQL List All Duplicates [duplicate]

蓝咒 提交于 2019-11-30 17:27:14
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

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.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!