Hi all, I have a mysql Database and 2 tables let's say clients and schools. Now each table has columns latitude and longitude. And I need do make a SELECT for example from second table where schools are in a given radius of one record from first table. Calculations should be made based on latitude and longitude. PS: I am using PHP.
+3
A:
You can calculate a distance using a Spherical law of cosines:
SELECT DEGREES(ACOS(SIN(RADIANS(clients.latitude)) * SIN(RADIANS(schools.latitude)) +
COS(RADIANS(clients.latitude)) * COS(RADIANS(schools.latitude))
* COS(RADIANS(clients.longitude–schools.longitude))))
* 60 * 1.1515 * 1.609344 AS distance
FROM clients, schools HAVING distance < $radius
RADIANS(X) - degrees to radians
ACOS(X) - the arc cosine of X, that is, the value whose cosine is X
DEGREES(X) - radians to degrees
60 - minutes in a degree
1.1515 - miles in a nautical mile
1.609344 - kilometres in a mile
Vitalii Fedorenko
2010-08-20 07:26:19
Thanks, I'll try now and accept if it's working.
Centurion
2010-08-20 07:36:43
So if my radius is in kilometers the query will be like you write or I should change it? thanks
Centurion
2010-08-20 07:40:25
Here the distance is calculated in kilometers
Vitalii Fedorenko
2010-08-20 07:44:02
Mysql gives an error : Unknown column 'clients.longitude–schools.longitude' in 'field list'..., what is the meaning of '-' sign here?
Centurion
2010-08-20 07:52:46
"-" is a minus sign
Vitalii Fedorenko
2010-08-20 08:01:38