jeudi 28 mai 2015

Please convert this query

"SELECT results.id, results.name, results.contact_phone, results.contact_email, results.verified_phone, results.verified_email, results.location_id, results.distance_in_km FROM (

  SELECT bands.id, bands.name,
          IF(bands.contact_phone > '', 'yes', 'no') AS contact_phone,
          IF(bands.contact_email > '', 'yes', 'no') AS contact_email,
          IF(bands.verified_phone=1, 'yes', 'no') AS verified_phone,
          IF(bands.verified_email=1, 'yes', 'no') AS verified_email,
          tags.name AS genre, locations.id AS location_id, p.*,
          ROUND((6378.10 * ACOS(COS(RADIANS(latpoint))
                     * COS(RADIANS(`lat`))
                     * COS(RADIANS(longpoint) - RADIANS(`long`))
                     + SIN(RADIANS(latpoint))
                     * SIN(RADIANS(`lat`)))), 0) AS distance_in_km
  FROM gigrep_promo_#{Rails.env}.taggings
    INNER JOIN gigrep_promo_#{Rails.env}.tags ON taggings.tag_id=tags.id
    INNER JOIN gigrep_promo_#{Rails.env}.bands ON taggings.taggable_id=bands.id
    INNER JOIN gigrep_promo_#{Rails.env}.locations ON bands.location_id=gigrep_promo_#{Rails.env}.locations.id
    JOIN (SELECT `lat` AS latpoint, `long` AS longpoint FROM gigrep_promo_#{Rails.env}.locations WHERE id=#{location_id} LIMIT 1) AS p
  WHERE context = 'genres'
    AND taggable_type = 'Band'
    #{genres_where}
  GROUP BY bands.name
  ORDER BY IF(distance_in_km IS NULL, 999999999, distance_in_km), bands.name
) AS results"

Aucun commentaire:

Enregistrer un commentaire