I need a function to be stored in MySQL. The function calculates the distance in miles between a latitude and longitude parameter and a "latitude" and "longitude" field in a database table. If the latitude or longitude in the database is 0, the function should return some large number (e.g. 99999999).
To improve calculation speed, a third parameter distance_limit is added to create a bounding box for the search, where a 0 value is ignored (all records are searched) and a non-zero number creates a box n miles in each direction. Latitudes or longitudes outside the box return 99999999, reducing the requirement to calculate those distances.
The function will be used (for example) as such:
SELECT DISTANCE([url removed, login to view], [url removed, login to view], 2) as dist, [url removed, login to view], [url removed, login to view] FROM users WHERE dist < 2.0