Based on:
- http://www.arubin.org/files/geo_search.pdf
- http://jehiah.cz/a/spatial-proximity-searching-using-latlongs
- http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/
- https://stackoverflow.com/questions/28847954/what-is-the-best-approach-to-find-all-addresses-that-are-in-a-specific-distance
SELECT rua,
truncate((degrees(acos(
sin(radians(latitude))
* sin( radians('-29.6395590'))
+ cos(radians(latitude))
* cos( radians('-29.6395590'))
* cos( radians(longitude - '-50.9884770') )
) ) * 111.045),1) as distance
FROM endereco
WHERE latitude BETWEEN '-29.6395590' - (50.0 / 111.045)
AND '-29.6395590' + (50.0 / 111.045)
AND longitude BETWEEN '-50.9884770' - (50.0 / (111.045 * COS(RADIANS('-29.6395590'))))
AND '-50.9884770' + (50.0 / (111.045 * COS(RADIANS('-29.6395590'))))
HAVING distance <= 2
ORDER BY distance;