在[member]表中,某些行对于电子邮件列具有相同的值。

login_id | email
---------|---------------------
john     | john123@hotmail.com
peter    | peter456@gmail.com
johnny   | john123@hotmail.com
...

有些人使用不同的login_id但相同的电子邮件地址,没有在这一列设置唯一约束。现在我需要找到这些行,看看是否应该删除它们。

我应该使用什么SQL语句来查找这些行?(MySQL 5)


当前回答

如果您的电子邮件列包含空值,则使用此选项

 select * from table where email in (
    select email from table group by email having count(*) > 1 and email != ''
    )

其他回答

这个查询会给你一个电子邮件地址的列表,以及它们被使用的次数,最常用的地址在前面。

SELECT email,
       count(*) AS c
FROM TABLE
GROUP BY email
HAVING c > 1
ORDER BY c DESC

如果你想要完整的行:

select * from table where email in (
    select email from table
    group by email having count(*) > 1
)

接受的答案的第一部分不工作的MSSQL。 这招对我很管用:

select email, COUNT(*) as C from table 
group by email having COUNT(*) >1 order by C desc

在这个线程的很晚,但我有一个类似的情况,以下工作在MySQL。下面的查询还将返回与重复电子邮件条件匹配的所有行

SELECT * FROM TABLE WHERE EMAIL IN 
       (SELECT * FROM 
            (SELECT EMAIL FROM TABLE GROUP BY EMAIL HAVING COUNT(EMAIL) > 1) 
        AS X);

如果您的电子邮件列包含空值,则使用此选项

 select * from table where email in (
    select email from table group by email having count(*) > 1 and email != ''
    )

这样效果最好

截图

SELECT RollId, count(*) AS c 
    FROM `tblstudents` 
    GROUP BY RollId 
    HAVING c > 1 
    ORDER BY c DESC