Skip to content

Instantly share code, notes, and snippets.

@BumbuKhan
Forked from esaounkine/distance_between.sql
Created September 19, 2017 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BumbuKhan/ce761f9baa4b347fb4c1a2059bbc5c1e to your computer and use it in GitHub Desktop.
Save BumbuKhan/ce761f9baa4b347fb4c1a2059bbc5c1e to your computer and use it in GitHub Desktop.
Calculate distance between two points with MySQL
CREATE FUNCTION distance_between (from_lat DECIMAL(6, 3), from_lng DECIMAL(6, 3), to_lat DECIMAL(6, 3), to_lng DECIMAL(6, 3)) RETURNS DECIMAL(11, 3)
RETURN 6371 * 2 * ATAN2(SQRT(POW(SIN(RADIANS(to_lat - from_lat)/2), 2) + POW(SIN(RADIANS(to_lng - from_lng)/2), 2) * COS(RADIANS(from_lat)) * COS(RADIANS(to_lat))), SQRT(1 - POW(SIN(RADIANS(to_lat - from_lat)/2), 2) + POW(SIN(RADIANS(to_lng - from_lng)/2), 2) * COS(RADIANS(from_lat)) * COS(RADIANS(to_lat))));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment