Find rows with multiple duplicate fields with Active Record, Rails & Postgres

前端 未结 5 1032
有刺的猬
有刺的猬 2020-12-04 06:07

What is the best way to find records with duplicate values across multiple columns using Postgres, and Activerecord?

I found this solution here:

User.f

5条回答
  •  死守一世寂寞
    2020-12-04 07:02

    If you need the full models, try the following (based on @newUserNameHere's answer).

    User.where(email: User.select(:email).group(:email).having("count(*) > 1").select(:email))
    

    This will return the rows where the email address of the row is not unique.

    I'm not aware of a way to do this over multiple attributes.

提交回复
热议问题