ruby-on-rails-3 - Rails 3 ActiveRecord 临时表

标签 ruby-on-rails-3 activerecord temp-tables

如何在 Rails 3 中执行以下操作?

CREATE TEMPORARY TABLE average_user_total_time 
  (SELECT SUM(time) AS time_taken 
        FROM scores 
        WHERE created_at >= '2010-10-10' 
                and created_at <= '2010-11-11' 
        GROUP BY user_id);

SELECT COUNT(*) from average_user_total_time WHERE time_taken > 60 and time_taken < 600

我尝试过做类似的事情

create_table (:average_user_total_time), :temporary=> true do |t|

end

但不知道如何准确使用它。我需要在我的应用程序中使用它,而不是在迁移中。

最佳答案

谢谢安德鲁,我最终采用了以下建议。

http://richtextblog.blogspot.com/2007/09/mysql-temporary-tables-and-rails.html

关于ruby-on-rails-3 - Rails 3 ActiveRecord 临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5413339/

相关文章:

mysql - 使用来自 1 个表的 1 列的变量的三个 MySQL 查询需要解析到临时表中

ruby-on-rails-3 - Fog S3 上传导致

ruby-on-rails - 每当 gem schedule.rb 文件 : doesn't recognize RAILS_ROOT variable

ruby-on-rails - HABTM 与 self 的关系?

mysql - 在 ActiveRecord 查询中包含所有 id

mysql - 来自 Yii2 中多个表的 SQL 请求

mysql - 使用 Cron Job 创建的 SQL 临时表的持续时间是多少

mysql - 通过 mariadb 中的用户变量通过 order by 和 row_number 进行排序

mysql - Redhat openshift - rails mysql 响应需要超过 20 秒

ruby - ActiveRecord Associations(has_one) - 访问父对象