加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

mysql – 如何在Rails3中的Active Record中按天和年分组

发布时间:2021-03-15 17:46:35 所属栏目:MySql教程 来源:网络整理
导读:我正在尝试在rails 3中重新创建以下mysql查询 select count(id),year(created_at),month(created_at) from table where published_state = 'published' group by year(created_at),month(created_at); 我尝试过类似的东西: results = Table.select('count(i

我正在尝试在rails 3中重新创建以下mysql查询

 select count(id),year(created_at),month(created_at) from table where published_state = 'published' group by year(created_at),month(created_at);

我尝试过类似的东西:

results = Table.select('count(id),month(created_at)').where('published_state LIKE ?','published').group('year(created_at),month(created_at)')

但它不会返回我想要的东西.

我只是得到了回报

[#

我怎么能做到这一点?

谢谢!

最佳答案 试试这个:

Table.where(:published_state => 'published').
  group('year(created_at)').group('month(created_at)').count(:id)

结果的格式并不是我所期望的(它是一个像[年,月]和计数值这样的数组键的哈希),但它可能会满足你的目的吗?

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读