Finding duplicate values in a SQL table

后端 未结 30 3982
南旧
南旧 2020-11-21 13:18

It\'s easy to find duplicates with one field:

SELECT name, COUNT(email) 
FROM users
GROUP BY email
HAVING COUNT(email) > 1

So if we have

30条回答
  •  隐瞒了意图╮
    2020-11-21 13:59

    The most important thing here is to have the fastest function. Also indices of duplicates should be identified. Self join is a good option but to have a faster function it is better to first find rows that have duplicates and then join with original table for finding id of duplicated rows. Finally order by any column except id to have duplicated rows near each other.

    SELECT u.*
    FROM users AS u
    JOIN (SELECT username, email
          FROM users
          GROUP BY username, email
          HAVING COUNT(*)>1) AS w
    ON u.username=w.username AND u.email=w.email
    ORDER BY u.email;
    

提交回复
热议问题