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

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

概述我正在尝试在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); 我尝试过类

我正在尝试在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)

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

以上是内存溢出为你收集整理的mysql – 如何在Rails3中的Active Record中按天和分组全部内容,希望文章能够帮你解决mysql – 如何在Rails3中的Active Record中按天和年分组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1166892.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)