Rails Mysql Group通过strftime查询不能在生产中工作

时间:2015-08-05 11:55:32

标签: mysql sql ruby-on-rails activerecord rails-activerecord

我在Rails中有一个查询,它在开发中而不是在生产中。查询是:

Event.where("created_at > ? AND name = ? AND game = ?", 3.month.ago, Event::TYPE_USER_SESSION, PLATFORM_CODE).group("strftime('%Y-%m-%d 00:00:00 UTC', created_at, '-6 days', 'weekday 0')").distinct.count(:user_id)

翻译为:

SELECT DISTINCT COUNT(DISTINCT "events"."user_id") AS count_user_id, strftime('%Y-%m-%d 00:00:00 UTC', created_at, '-6 days', 'weekday 0') AS strftime_y_m_d_00_00_00_utc_created_at_6_days_weekday_0 FROM "events" WHERE (created_at > '2015-05-05 11:51:32.292908' AND name = 'user_session' AND game = 'LK') GROUP BY strftime('%Y-%m-%d 00:00:00 UTC', created_at, '-6 days', 'weekday 0')

开发 工作与sqlite3,但在使用mysql生产时,我收到此错误:

ActiveRecord::StatementInvalid: Mysql2::Error: FUNCTION monkiland.strftime does not exist: SELECT DISTINCT COUNT(DISTINCT `events`.`user_id`) AS count_user_id, strftime('%Y-%m-%d 00:00:00 UTC', created_at, '-6 days', 'weekday 0') AS strftime_y_m_d_00_00_00_utc_created_at_6_days_weekday_0 FROM `events` WHERE (created_at > '2015-05-05 11:52:09.879454' AND name = 'user_session' AND game = 'LK') GROUP BY strftime('%Y-%m-%d 00:00:00 UTC', created_at, '-6 days', 'weekday 0')

在我的gemfile中

group :production do
  gem 'mysql2', '0.3.17'
end

group :development do
  gem 'sqlite3'
end

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

使用Data_Format()函数它类似于strftime()。