title: Haversine's formula in sqlalchemy
body: <p>I'm having the following lines in my code</p>
  
  <pre><code>    query = "SELECT id, " \
          "( 3959 * acos( cos( radians(37) ) * cos( radians( %(lat)i ) ) * " \
          "cos( radians( %(lng)i ) - radians(-122) ) + sin( radians(37) ) * " \
          "sin( radians( %(lat)i ) ) ) ) AS `distance` from message where" \
          " `distance` &lt;= %(drange)d" % {'lat': float(lat), 'lng': float(lng), 'drange': int(drange)}
      print query
      messages = db.session.query(Message).from_statement(query).all()
  </code></pre>
  
  <p>I get the following error when I use this</p>
  
  <pre><code>OperationalError: (OperationalError) (1054, "Unknown column 'distance' in 'where clause'") 'SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( 0 ) ) * cos( radians( 0 ) - radians(-122) ) + sin( radians(37) ) * sin( radians( 0 ) ) ) ) AS `distance` from message where `distance` &lt;= 50' ()
  </code></pre>
  
  <p>What is the correct way to fix this?</p>