首页 文章

查找MySQL中列上具有相同值的行

提问于
浏览
163

在[member]表中,某些行具有 email 列的相同值 .

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

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

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

7 回答

  • 2

    这是查询 email 的查询,它们用于多个 login_id

    SELECT email
    FROM table
    GROUP BY email
    HAVING count(*) > 1
    

    您将需要第二个(嵌套的)查询来获取 login_idemail 列表 .

  • 3

    接受的答案的第一部分对MSSQL不起作用 .
    这对我有用:

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

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

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

    谢谢你们:-)我使用下面的内容,因为我只关心这两个列,而不是其余的 . 工作得很好

    select email, login_id from table
        group by email, login_id
        having COUNT(email) > 1
    
  • 11

    我知道这是一个非常古老的问题,但对于可能遇到同样问题的其他人来说更是如此,我认为这对于想要的更准确 .

    SELECT * FROM member WHERE email = (Select email From member Where login_id = john123@hotmail.com)
    

    这将返回所有具有john123@hotmail.com作为login_id值的记录 .

  • 2

    此查询将为您提供电子邮件地址列表及其使用次数,首先使用最常用的地址 .

    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
    )
    
  • 9
    select email from mytable group by email having count(*) >1
    

相关问题