我发现我有许多用户对象具有相同的电子邮件地址。我需要删除那些重复项。
User.select(:email).group(:email).having('COUNT(email) > 1')
我尝试了以下查询(类似于此处的上一个问题)。但我得到一个空数组。知道为什么吗?
2.0.0p247 :277 > User.select(:email).group(:email).having('COUNT(email) > 1')
User Load (7801.4ms) SELECT email FROM "users" GROUP BY email HAVING COUNT(email) > 1
EXPLAIN (0.4ms) EXPLAIN SELECT email FROM "users" GROUP BY email HAVING COUNT(email) > 1
EXPLAIN for: SELECT email FROM "users" GROUP BY email HAVING COUNT(email) > 1
QUERY PLAN
-----------------------------------------------------------------------------
GroupAggregate (cost=676876.34..739393.66 rows=3125866 width=22)
Filter: (count(email) > 1)
-> Sort (cost=676876.34..684691.01 rows=3125866 width=22)
Sort Key: email
-> Seq Scan on users (cost=0.00..147342.66 rows=3125866 width=22)
(5 rows)
=> []
更新另外,如果我尝试 Dave 的解决方案,它也不起作用。
2.0.0p247 :004 > User.select('email, count(email)').group('email').having('count(email) > 1')
User Load (7858.0ms) SELECT email, count(email) FROM "users" GROUP BY email HAVING count(email) > 1
EXPLAIN (0.4ms) EXPLAIN SELECT email, count(email) FROM "users" GROUP BY email HAVING count(email) > 1
EXPLAIN for: SELECT email, count(email) FROM "users" GROUP BY email HAVING count(email) > 1
QUERY PLAN
-----------------------------------------------------------------------------
GroupAggregate (cost=676876.34..747208.33 rows=3125866 width=22)
Filter: (count(email) > 1)
-> Sort (cost=676876.34..684691.01 rows=3125866 width=22)
Sort Key: email
-> Seq Scan on users (cost=0.00..147342.66 rows=3125866 width=22)
(5 rows)