Finding duplicate values in a SQL table

后端 未结 30 4050
南旧
南旧 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 14:04

    To Check From duplicate Record in a table.

    select * from users s 
    where rowid < any 
    (select rowid from users k where s.name = k.name and s.email = k.email);
    

    or

    select * from users s 
    where rowid not in 
    (select max(rowid) from users k where s.name = k.name and s.email = k.email);
    

    To Delete the duplicate record in a table.

    delete from users s 
    where rowid < any 
    (select rowid from users k where s.name = k.name and s.email = k.email);
    

    or

    delete from users s 
    where rowid not in 
    (select max(rowid) from users k where s.name = k.name and s.email = k.email);
    

提交回复
热议问题