How to find duplicate records in PostgreSQL

前端 未结 4 1437
长情又很酷
长情又很酷 2021-01-29 17:17

I have a PostgreSQL database table called \"user_links\" which currently allows the following duplicate fields:

year, user_id, sid, cid

The uni

4条回答
  •  时光取名叫无心
    2021-01-29 18:09

    The basic idea will be using a nested query with count aggregation:

    select * from yourTable ou
    where (select count(*) from yourTable inr
    where inr.sid = ou.sid) > 1
    

    You can adjust the where clause in the inner query to narrow the search.


    There is another good solution for that mentioned in the comments, (but not everyone reads them):

    select Column1, Column2, count(*)
    from yourTable
    group by Column1, Column2
    HAVING count(*) > 1
    

    Or shorter:

    SELECT (yourTable.*)::text, count(*)
    FROM yourTable
    GROUP BY yourTable.*
    HAVING count(*) > 1
    

提交回复
热议问题