Skip to content

Instantly share code, notes, and snippets.

@jadwigo
Last active April 28, 2020 20:59
Show Gist options
  • Select an option

  • Save jadwigo/dd24fe4285582a00dbb2eede878d0f9b to your computer and use it in GitHub Desktop.

Select an option

Save jadwigo/dd24fe4285582a00dbb2eede878d0f9b to your computer and use it in GitHub Desktop.
Query locations in a circle of $max_distance around $input_latitude, $input_longitude
SELECT
l.*,
(
6371
* acos(
cos(
radians( $input_latitude )
)
* cos(
radians( l.latitude )
)
* cos(
radians( l.longitude ) - radians( $input_longitude )
)
+ sin(
radians( $input_latitude )
)
* sin(
radians( l.latitude )
)
)
) AS distance
FROM locations l
WHERE
l.id != 0 -- or some other conditions thate make more sense
HAVING distance < $max_distance
ORDER BY distance
LIMIT 0, 10;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment