如何使用sql和绑定查找?

时间:2013-10-20 16:00:02

标签: sql ruby-on-rails activerecord

我一直在盯着我的代码超过一个小时,我仍然无法弄清楚我的查询有什么问题。 我收到错误消息: ActiveRecord :: StatementInvalid:PG :: SyntaxError:ERROR:“或”附近的语法错误 第38行:WHERE(abs(ax2 - ?)/?<?)

query = %Q{

  SELECT 

    avg(ax2), 
    avg(ay2), 
    avg(az2) 

  FROM (

    SELECT 
      avg(t1) as t2,
      avg(n1) as n2,
      avg(ax1) as ax2,
      avg(ay1) as ay2,
      avg(az1) as az2

    FROM ( 

      SELECT  
        avg(t) as t1, 
        avg(n) as n1, 
        avg(ax) as ax1, 
        avg(ay) as ay1, 
        avg(az) as az1 

      FROM obds 
      WHERE uid = #{uid}  AND (ev = 6011 or ev = 6012)
      GROUP BY 
        round(t,3), 
        round(n,3)
    ) 

    AS derivedTable1 
    GROUP BY round(t1 + 0.0005,3), round(n1 + 0.0005,3)
  ) 
  AS derivedTable2
  WHERE (abs(ax2 - ?) / ? < ?)  #<<<<< Line with error
}

binds = [ uid, x_average, x_average, THRESHOLD_FOR_RESTING ]
result = Ozd.find_by_sql query, binds

2 个答案:

答案 0 :(得分:1)

答案是查询必须是绑定数组的一部分。

 binds = [ query, uid, x_average, x_average, THRESHOLD_FOR_RESTING ]
 results = Ozd.find_by_sql binds

答案 1 :(得分:0)

尝试此WHERE (abs((ax2 - ?) / ? < ?))而不是