Skip to content

Instantly share code, notes, and snippets.

@kenmickles
Created March 1, 2012 15:18
Show Gist options
  • Save kenmickles/1950453 to your computer and use it in GitHub Desktop.
Save kenmickles/1950453 to your computer and use it in GitHub Desktop.
Slow sort MySQL records by distance
SELECT
name,
( 3959 * acos( cos( radians({$lat}) ) * cos( radians( latitude ) )
* cos( radians(longitude) - radians({$lng})) + sin(radians({$lat}))
* sin( radians(latitude)))) AS distance
FROM {$table}
HAVING distance < 1
ORDER BY distance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment