0

我有一个不能与 postgresql 一起使用的 mysql 查询:-

@events = Event.all_with_distance([current_user.geo_lat, current_user.geo_lng]).where("start > ?", Time.zone.now).order("distance").order("start DESC").where("title like ?", "%#{params[:q]}%")

def self.all_with_distance(origin)
    distance_sql = sql_for_distance(origin)
    select("#{table_name}.*, #{distance_sql} AS distance").select("`locations`.`geo_lat`, `locations`.`geo_lng`, `locations`.`name` as location_name").joins(:location).where("#{distance_sql} < 50")
  end

这是我得到的错误:-

PG::Error: ERROR:  syntax error at or near "."
LINE 1: ...SIN(RADIANS(26.8465108)) * SIN(RADIANS(`locations`.`geo_lat`...

生成的查询:-

SELECT events.*, (ACOS( SIN(RADIANS(26.8465108)) * SIN(RADIANS(`locations`.`geo_lat`)) + COS(RADIANS(26.8465108)) * COS(RADIANS(`locations`.`geo_lat`)) * COS(RADIANS(`locations`.`geo_lng`) - RADIANS(80.9466832)) ) * 6378.135)  AS distance, `locations`.`geo_lat`, `locations`.`geo_lng`, `locations`.`name` as location_name FROM "events" INNER JOIN "locations" ON "locations"."id" = "events"."location_id" WHERE ((ACOS( SIN(RADIANS(26.8465108)) * SIN(RADIANS(`locations`.`geo_lat`)) + COS(RADIANS(26.8465108)) * COS(RADIANS(`locations`.`geo_lat`)) * COS(RADIANS(`locations`.`geo_lng`) - RADIANS(80.9466832)) ) * 6378.135)  < 50) AND (start > '2012-09-21 06:40:18.964016') AND (title like '%%') ORDER BY distance, start DESC
4

1 回答 1

0

是的,Back Ticks 是 MySql 主义

用“”删除''后它工作正常。

这是更改后的查询:-

select("#{table_name}.*, #{distance_sql} AS distance").select('"locations"."geo_lat", "locations"."geo_lng", "locations"."name" as location_name').joins(:location).where("#{distance_sql} < 50")
于 2012-09-21T10:47:18.683 回答