"整理" GROUP BY"的&QUOT结果;计数? [英] "Order by" result of "group by" count?

查看:149
本文介绍了"整理" GROUP BY"的&QUOT结果;计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此查询

Message.where("message_type = ?", "incoming").group("sender_number").count

将返回我的哈希值。

will return me an hash.

OrderedHash {"1234"=>21, "2345"=>11, "3456"=>63, "4568"=>100}

现在我想通过各组数订购。我怎样才能做到这一点在查询中。

Now I want to order by count of each group. How can I do that within the query.

推荐答案

要做到这一点,最简单的方法是把一个订单子句添加到原始查询。如果你给计数方法的具体领域,它会生成一个名为count_ {}栏,可以通过添加的顺序调用生成的SQL中使用的输出列:

The easiest way to do this is to just add an order clause to the original query. If you give the count method a specific field, it will generate an output column with the name count_{column}, which can be used in the sql generated by adding an order call:

Message.where('message_type =?','来电')。组(sender_number)。为了(count_id ASC)。COUNT('身份证')

这篇关于"整理" GROUP BY"的&QUOT结果;计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆