mysql - Sql 到 Rails 查询关联、连接和 order_by

标签 mysql join ruby-on-rails-4 associations rails-activerecord

我想将sql查询转换为rails查询

SQL:

选择计数(*),created_at来自订阅,其中account_id=(从帐户其中google_domain选择ID IS NOT NULL) GROUP BYcreated_atORDER BYcreated_atDESC;

我尝试了这个,但没有得到确切的结果。

我的部分解决方案::

Subscription.joins(:account).where('accounts.google_domain is not null').count

这里我不知道如何使用分组和排序依据。

如果需要任何信息,请发表评论。提前致谢

最佳答案

Subscription.joins(:account).order("您的需求").group("您的需求").where("accounts.google_domain IS NOT NULL").count

希望对您有帮助

关于mysql - Sql 到 Rails 查询关联、连接和 order_by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26646596/

相关文章:

php - PHP时间戳和mysql时间戳的区别

php - 我将如何在非 laravel 应用程序的子文件夹中使用 Laravel 完整框架

mysql - SQLSTATE 错误语法错误或访问冲突 : 1066 Not unique table/alias: 'users_sessions'

PHP crypt() 和 Coldfusion

MySQL 如果没有匹配则返回默认记录(没有 LIMIT)

MySQL 在一个表上合并两个带有 CASE 的 SELECT 查询

Mysql模拟FULL OUTER JOIN

ruby - 如何总结所有哈希值?

ruby-on-rails - 单表继承和 has_many 问题

javascript - Rails VideoJS swf-Flash-后备 asset_path