Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Stephen Blackwell stevehb

🎯
Focusing
View GitHub Profile
@stevehb
stevehb / nearby-coordinates.sql
Created Jan 21, 2019 — forked from statickidz/nearby-coordinates.sql
Ordering with SQL by nearest latitude & longitude coordinates (MySQL & SQLite)
View nearby-coordinates.sql
---
METHOD 1
This should roughly sort the items on distance in MySQL, and should work in SQLite.
If you need to sort them preciser, you could try using the Pythagorean theorem (a^2 + b^2 = c^2) to get the exact distance.
---
SELECT *
FROM table
ORDER BY ((lat-$user_lat)*(lat-$user_lat)) + ((lng - $user_lng)*(lng - $user_lng)) ASC
You can’t perform that action at this time.