Getting Distance between 2 points with MySQL Query
Calculating the distance between 2 points (longitude and latitude) certainly sounds like something that should be solved on the code layer, not on the database layer.
However, more often than not, it's a lot faster to write it inside a query and get the result for a big list of locations.
So how is it done?
By using the Haversine formula (it's not hard, I swear).
To calculate the great-circle distance of two points on a sphere
(for more information, please go to your local library, if year > 2004, check wikipedia)
The implementation of which (in a MySQL query), looks like this:
|
|
(6353 - Is the Earth's Radius (in KM))
You can test it out, for instance, let's get the distance between
the Eiffel Tower (48.858278,2.294254) and Big Ben (51.500705,-0.124575),
which should be about 340 KM aerial distance (man Europe is small!)
so running our query with the Longitudes and Latitudes set:
|
|
gets us 339.58.
As you can see I used the ROUND(..,2) function to make the result a bit more readable.
If you want to use this often, you might want to write a function that calculates distance:
|
|
That's better, now we can just use the function in queries to quickly calculate distances.
As a side note:
I do highly recommend using MySQL Spatial extensions for more complex calculations, though
honestly it seems that PostgreSQL has the upper hand in that area.