Skip to content

Instantly share code, notes, and snippets.

@gergelypolonkai
Created April 27, 2015 07:59
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 gergelypolonkai/bdad1cf2d410853bef35 to your computer and use it in GitHub Desktop.
Save gergelypolonkai/bdad1cf2d410853bef35 to your computer and use it in GitHub Desktop.
MySQL Haversine function
DELIMITER $$
CREATE FUNCTION `haversine` (lng1 FLOAT, lat1 FLOAT, lng2 FLOAT, lat2 FLOAT)
RETURNS float NO SQL DETERMINISTIC
BEGIN
SET @a = ABS(POWER(SIN(RADIANS(lat1 - lat2)) / 2, 2) + COS(RADIANS(lat1)) * COS(RADIANS(lat2)) * POWER(SIN(RADIANS(lng1 - lng2)) / 2, 2));
RETURN 12756.200 * ATAN2(SQRT(@a), SQRT(1 - @a));
END$$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment