I also searched around but found nothing that really helped. Here's the situation:
Let's say I have a simple relationship for cities and weather predictions. We can relate with the latitude and longitude, so we could have the following:
CITY TABLE:
cityId | name | lat | lng
=====================================================
1 | Barcelona | -33.46773911 | 151.38214111
2 | London | 46.57906604 | 11.24854176
3 | Paris | 20.38509560 | -99.95350647
4 | Madrid | 44.38623047 | 6.64792013
WEATHER PREDICTION TABLE:
weatherId | date | prediction | lat | lng
=====================================================================
1 | 2015-01-01 | SUN | -33.36773911 | 151.28214111
2 | 2015-01-02 | CLOUD | -33.36773911 | 151.28214111
3 | 2015-01-01 | RAIN | 44.37623047 | 6.64792013
I have this query to get the closest record to Barcelona in (2015-01-01):
SELECT prediction, lat, lng, (6371 * acos(cos(radians(-33.46773911)) * cos(radians(lat)) * cos(radians(lng) - radians(151.38214111)) + sin(radians(-33.46773911)) * sin(radians(lat)))) as radius
FROM weather
WHERE
(lat between -33.06773911 and -33.56773911) AND
(lng between 151.08214111 and 151.58214111) AND
date = '2015-01-01'
HAVING
radius IS NOT NULL AND radius <= 2000
ORDER BY
radius ASC
LIMIT 1
But, What's the most efficient way to query for return all cities with the most near weather prediction point for a date, like this:
PREDICTION FOR (2015-01-01):
cityId | name | lat | lng | prediction
==================================================================
1 | Barcelona | -33.46773911 | 151.38214111 | SUN
2 | London | 46.57906604 | 11.24854176 | RAIN
3 | Paris | 20.38509560 | -99.95350647 | RAIN
4 | Madrid | 44.38623047 | 6.64792013 | RAIN