0

レポート専用の中間結果を含むテーブルを作成してみます。私がやっていることは次のとおりです。

 ActiveRecord::Base.connection.execute(sql_query)

sql_query は次のとおりです。

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 ;

毎回エラーが出ます

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クライアントに渡すと、正しく機能します。

私が間違っていることと、ActiveRecordでカップルSQLステートメントを実行する方法

4

2 に答える 2

3

ActiveRecord の MySQL 接続は、デフォルトで 1 つのステートメントしか実行できません。複数のステートメントを実行するCLIENT_MULTI_STATEMENTSには、接続の作成時に (65536) をオプションとして渡す必要があります。コードは次のようになります。

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

ConnectionAdapters::MysqlAdapter.newmysql gem で見つけて、そのメソッドを でオーバーライドできますconfig/initializersここに詳細なガイドがあります。

于 2012-10-22T13:32:57.853 に答える
1

クエリをエスケープしようとしましたか?

DROP TABLE IF EXISTS `_by_people_daily`

アンダースコアで始まる表は、私には疑わしいように見えます。

アップデート:

Google で見つけたものを見てください: http://www.seanr.ca/tech/?p=75

于 2012-10-22T13:09:32.243 に答える