Re: haversine formula with postgreSQL - Mailing list pgsql-general

From Jonathan
Subject Re: haversine formula with postgreSQL
Date
Msg-id 1fc16f45-f2c0-4a56-96e0-5a649f92b25f@o21g2000vbl.googlegroups.com
Whole thread Raw
In response to haversine formula with postgreSQL  (Jonathan <jharahush@gmail.com>)
List pgsql-general
It's the whole query as far as I can tell.  The app takes input from
the user --- the user enters an address and chooses a radius ("show me
all facilities within 5 miles of this address") and then the latitude
and longitude of the address and the radius is passed into the query
so that the database can grab all locations within a certain radius.

The example provided by Google is using MySQL.  The query for MySQL
looks like this:

SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) *
cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin
( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers
HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20

And I'm attempting to change it to work with Postgres and have done
this:

SELECT 'ID', 'FACILITY', 'ADDRESS', latitude, longitude, ( 3959 * acos
( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians
( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians
( latitude ) ) ) ) AS distance FROM aaafacilities HAVING distance <
'%s' ORDER BY distance LIMIT 0 OFFSET 20


aaafacilities is my table name in my posgres database.

I'm sorry if this isn't enough info.. like I said, I'm new to this but
definitely interested in learning and figuring this out!

From what I can tell, the database is supposed to calculate and then
output the distance of each "match" but it seems like in the MySQL
example, it can do this without having an actual distance column in
the database.

Thanks again!

pgsql-general by date:

Previous
From: Mike Christensen
Date:
Subject: Re: haversine formula with postgreSQL
Next
From: Tom Lane
Date:
Subject: Re: haversine formula with postgreSQL