在rails中计算和按月分组名称

时间:2014-03-14 22:34:04

标签: mysql ruby-on-rails-3

这是我的代码

 MediaDownload.select("COUNT(*) AS count_all,MONTHNAME(created_at) AS created_at ").group('created_at').count

但不是像这样得到一个sql(mysql)代码:

 SELECT COUNT(*) AS count_all, MONTHNAME(created_at) AS created_at FROM `media_downloads` GROUP BY created_at

我明白了:

 SELECT COUNT(*) AS count_all, created_at AS created_at FROM `media_downloads` GROUP BY created_at

我做错了什么。有什么解决方案可以得到我想要的东西。

1 个答案:

答案 0 :(得分:1)

ActiveRecord查询语法中存在多余的count,因此以下为

MediaDownload.select("COUNT(*) AS count_all,MONTHNAME(created_at) AS created_at ").group('created_at')

生成你想要的SQL查询