Find rows that have the same value on a column in MySQL

前端 未结 8 1076
情书的邮戳
情书的邮戳 2020-11-30 16:27

In a [member] table, some rows have the same value for the email column.

login_id | email
---------|---------------------
john     | john123@hot         


        
相关标签:
8条回答
  • 2020-11-30 17:00

    Get the entire record as you want using the condition with inner select query.

    SELECT *
    FROM   member
    WHERE  email IN (SELECT email
                     FROM   member
                     WHERE  login_id = abcd.user@hotmail.com) 
    
    0 讨论(0)
  • 2020-11-30 17:02

    This query will give you a list of email addresses and how many times they're used, with the most used addresses first.

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

    If you want the full rows:

    select * from table where email in (
        select email from table
        group by email having count(*) > 1
    )
    
    0 讨论(0)
  • 2020-11-30 17:04

    use this if your email column contains empty values

     select * from table where email in (
        select email from table group by email having count(*) > 1 and email != ''
        )
    
    0 讨论(0)
  • 2020-11-30 17:08

    Here is query to find email's which are used for more then one login_id:

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

    You'll need second (of nested) query to get list of login_id by email.

    0 讨论(0)
  • 2020-11-30 17:12

    Thanks guys :-) I used the below because I only cared about those two columns and not so much about the rest. Worked great

      select email, login_id from table
        group by email, login_id
        having COUNT(email) > 1
    
    0 讨论(0)
  • 2020-11-30 17:23

    First part of accepted answer does not work for MSSQL.
    This worked for me:

    select email, COUNT(*) as C from table 
    group by email having COUNT(*) >1 order by C desc
    
    0 讨论(0)
提交回复
热议问题