Finding duplicate values in a SQL table

后端 未结 30 4086
南旧
南旧 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:03

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

    Simply group on both of the columns.

    Note: the older ANSI standard is to have all non-aggregated columns in the GROUP BY but this has changed with the idea of "functional dependency":

    In relational database theory, a functional dependency is a constraint between two sets of attributes in a relation from a database. In other words, functional dependency is a constraint that describes the relationship between attributes in a relation.

    Support is not consistent:

    • Recent PostgreSQL supports it.
    • SQL Server (as at SQL Server 2017) still requires all non-aggregated columns in the GROUP BY.
    • MySQL is unpredictable and you need sql_mode=only_full_group_by:
      • GROUP BY lname ORDER BY showing wrong results;
      • Which is the least expensive aggregate function in the absence of ANY() (see comments in accepted answer).
    • Oracle isn't mainstream enough (warning: humour, I don't know about Oracle).

提交回复
热议问题