问题描述
我试图找到超过3M表,所有谁拥有相同的用户名的用户。我看过这样的事情可以做的伎俩。
I try to find over a 3M table, all the users who have the same username. I read something like this may do the trick.
User.find(:all, :group => [:username], :having => "count(*) > 1" )
不过,由于我使用的Postgres这回我的ActiveRecord :: StatementInvalid:PG ::错误:错误:列users.id必须出现在GROUP BY子句中,或在聚合中使用功能。
我想这样的事情
User.select('users.id, users.username').having("count(*) > 1").group('users.username')
但仍然得到同样的错误。知不知道我做错了什么?
But still get the same error. Any idea what I'm doing wrong?
更新:('。用户*)。我做了它在某种程度上工作中使用 User.select组(users.id')有('计数(用户.username)> 1')
但此查询将返回我这看起来像一个空数组即使是建党5个记录
Update: I made it somehow work using User.select('users.*').group('users.id').having('count(users.username) > 1')
but this query returns me this which looks like an empty array even if is founding 5 records.
GroupAggregate (cost=9781143.40..9843673.60 rows=3126510 width=1365)
Filter: (count(username) > 1)
-> Sort (cost=9781143.40..9788959.68 rows=3126510 width=1365)
Sort Key: id
-> Seq Scan on users (cost=0.00..146751.10 rows=3126510 width=1365)
(5 rows)
=> []
任何想法,为什么发生这种情况,以及如何获得这些5行?
Any idea why this is happening and how to get those 5 rows?
推荐答案
我认为最好的,你可以得到的是获得用户名重复的记录。可与实现
I think the best you could get is to get usernames for duplicate records. That can be achieved with
User.select(:username).group(:username).having('COUNT(username) > 1')
这篇关于如何做到在Postgres的这组ActiveRecord的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!