Last active
April 28, 2020 20:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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