title: How can I make this geo-distance SQL query Postgres compatible
body: <p>A library I’m trying to use takes a given latitude and longitude and work out with entries in a table have a lat/lng within a certain distance from this. The generated SQL query works with MySQL, but not with PostgreSQL.</p>
  
  <p>Here’s the entry in my <code>server.log</code> file detailing the error <code>psql</code> is giving and the full query:</p>
  
  <pre><code>ERROR:  column "distance" does not exist at character 507
  STATEMENT:  select *, ( '3959' * acos( cos( radians('53.49') ) * cos( radians( places.lat ) ) * cos( radians( places.lng ) - radians('-2.38') ) + sin( radians('53.49') ) * sin( radians( places.lat ) ) ) ) AS distance from (
                          Select *
                          From places
                          Where places.lat Between 53.475527714192 And 53.504472285808
                          And places.lng Between -2.4043246788967 And -2.3556753211033
                      ) As places where "places"."deleted_at" is null having "distance" &lt;= $1 order by "distance" asc
  </code></pre>
  
  <p>Knowing what the SQL should be I can then edit the PHP code that generates it and send a PR back to the library.</p>