In PHP, I have the following code for calculating the distance between two locations:
Maybe something like
SELECT field1, field2, ...,
ACOS(SIN(latitude / 180 * PI()) * SIN(:1) + COS(latitude / 180 * PI()) * COS(:2) * COS(:2 - longtidude)) * 6371 AS distance
ORDER BY distance ASC;
or
SELECT field1, field2, ...,
ACOS(SIN(RADIANS(latitude)) * SIN(:1) + COS(RADIANS(latitude)) * COS(:2) * COS(:2 - longtidude)) * 6371 AS distance
ORDER BY distance ASC;
(directly translated from the PHP code)
:1
and :2
is $lat2/180*pi()
and $long2/180*pi()
respectively.