ActiveRecord的执行原始SQL [英] ActiveRecord execute raw sql

查看:371
本文介绍了ActiveRecord的执行原始SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用中间结果specialy报表创建表。 我正在做的事情是:

I try create table with intermediate results specialy for report. What I'm doing is:

 ActiveRecord::Base.connection.execute(sql_query)

sql_query是:

sql_query is:

SET SQL_SAFE_UPDATES=0;

      DROP TABLE IF EXISTS _by_people_daily;

      CREATE TEMPORARY TABLE _by_people_daily
      AS
      SELECT #{date_int} AS date_int,
             memberships.user_id AS user_id,
             ci.community_id,
             ci.content_id,
             contents.composite_type AS content_type,

             COUNT(views.id) AS views,
             COUNT(comments.id) AS comments,
             COUNT(shares.id) AS shares,
             COUNT(likes.id) AS likes,
             COUNT(uploads.id) AS uploads
      FROM community_items AS ci
        JOIN memberships ON memberships.community_id = ci.community_id
        JOIN contents ON ci.content_id = contents.id
        JOIN (SELECT '#{day_begin}' as start_date, '#{day_end}' as end_date) AS dates
        LEFT JOIN contents AS uploads
               ON uploads.id = ci.content_id
              AND uploads.user_id = memberships.user_id
              AND uploads.created_at BETWEEN dates.start_date AND dates.end_date
        LEFT JOIN comments
               ON comments.content_id = ci.content_id
              AND comments.user_id = memberships.user_id
              AND comments.created_at BETWEEN dates.start_date AND dates.end_date
        LEFT JOIN shares
               ON shares.content_id = ci.content_id
              AND shares.user_id = memberships.user_id
              AND shares.created_at BETWEEN dates.start_date AND dates.end_date
        LEFT JOIN likes
               ON likes.content_id = ci.content_id
              AND likes.user_id = memberships.user_id
              AND likes.created_at BETWEEN dates.start_date AND dates.end_date
        LEFT JOIN views
               ON views.viewable_id = ci.content_id
              AND views.viewable_type = 'Content'
              AND views.user_id = memberships.user_id
              AND views.created_at BETWEEN dates.start_date AND dates.end_date
      WHERE COALESCE(views.id, comments.id, shares.id, likes.id, uploads.id ) IS NOT NULL
        #{ " AND users.company_id = #{@company.id} " if @company }
      GROUP BY memberships.user_id, ci.community_id, ci.content_id, contents.composite_type ;

      DELETE FROM intermediate_by_people WHERE date_int = #{date_int} ;

      INSERT INTO intermediate_by_people
            (date_int, user_id, community_id, content_id, content_type, views, comments, shares, uploads, likes)
      SELECT date_int, user_id, community_id, content_id, content_type, views, comments, shares, uploads, likes
      FROM _by_people_daily ;

我每次收到错误

I'm getting error every time

ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DROP TABLE IF EXISTS _by_people_daily;

但是当我通过查询到MySQL客户端它正常工作。

But when I pass query to mysql client it working correctly.

我在做什么错误以及如何与ActiveRecord的执行几个SQL语句

What I'm doing wrong and how execute couple sql statements with ActiveRecord

推荐答案

的ActiveRecord的MySQL连接只能在默认情况下执行一个语句。要执行多条语句, CLIENT_MULTI_STATEMENTS (65536)需要传递作为一个选项创建连接时。在code可能是这样的:

ActiveRecord's MySQL connection can only execute one statement by default. To execute multiple statements, CLIENT_MULTI_STATEMENTS(65536) need to be passed as an option when creating the connection. The code may look like this:

ConnectionAdapters::MysqlAdapter.new(mysql, logger, [host, username, password, database, port, socket, 65536], config)

您可以找到 ConnectionAdapters :: MysqlAdapter.new 在你的mysql的宝石,并覆盖配置/初始化。 这里是一个详细的指南。

You can find ConnectionAdapters::MysqlAdapter.new in your mysql gem, and override that method in config/initializers. Here is a detailed guide.

这篇关于ActiveRecord的执行原始SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆