[英]ActiveRecord execute raw sql
I try create table with intermediate results specialy for report. 我尝试创建带有中间结果的表,专门用于报告。 What I'm doing is:
我正在做的是:
ActiveRecord::Base.connection.execute(sql_query)
sql_query is: 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 ;
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;
But when I pass query to mysql client it working correctly. 但是,当我将查询传递给mysql客户端时,它可以正常工作。
What I'm doing wrong and how execute couple sql statements with ActiveRecord 我在做什么错以及如何使用ActiveRecord执行几个SQL语句
ActiveRecord's MySQL connection can only execute one statement by default. 默认情况下,ActiveRecord的MySQL连接只能执行一条语句。 To execute multiple statements,
CLIENT_MULTI_STATEMENTS
(65536) need to be passed as an option when creating the connection. 要执行多个语句,在创建连接时需要将
CLIENT_MULTI_STATEMENTS
(65536)作为选项传递。 The code may look like this: 代码可能看起来像这样:
ConnectionAdapters::MysqlAdapter.new(mysql, logger, [host, username, password, database, port, socket, 65536], config)
You can find ConnectionAdapters::MysqlAdapter.new
in your mysql gem, and override that method in config/initializers
. 您可以在mysql gem中找到
ConnectionAdapters::MysqlAdapter.new
,并在config/initializers
覆盖该方法。 Here is a detailed guide. 这是详细的指南。
Did you try escaping your query? 您是否尝试转义查询?
DROP TABLE IF EXISTS `_by_people_daily`
A table beginning with a underscore looks suspicious to me. 以下划线开头的表格对我来说可疑。
Update: 更新:
Look what I have found via google: http://www.seanr.ca/tech/?p=75 看看我通过Google找到的内容: http : //www.seanr.ca/tech/? p= 75
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.