0

我想做这样的事情

User.select("users.id, count(whatever) as test")

但是 rails 只返回 User.id 而没有计数。我不能使用类似的东西

User.count(whatever)

因为我在一个非常复杂的 SQL 查询中使用了这个 select。是否可以使用 select helper 获取计数值?

4

1 回答 1

1

您确定以下查询返回User.id吗?它不会抛出错误说“列在选择列表中无效,因为它既不包含在聚合函数或 group by 子句中”:

User.select("users.id, count(whatever) as test")

因为您正在使用count,您还应该使用group以下方法:

User.select("users.id, count(whatever) as test").group("users.id, whatever")

然后你的结果数组将包含users.idand count(whatever)

于 2013-07-03T20:02:22.973 回答