Skip to content

Instantly share code, notes, and snippets.

View stevehb's full-sized avatar
🎯
Focusing

Stephen Blackwell stevehb

🎯
Focusing
View GitHub Profile
@stevehb
stevehb / nearby-coordinates.sql
Created January 21, 2019 03:18 — forked from statickidz/nearby-coordinates.sql
Ordering with SQL by nearest latitude & longitude coordinates (MySQL & SQLite)
---
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