Skip to content

Instantly share code, notes, and snippets.

statickidz / nearby-coordinates.sql
Last active Apr 16, 2021
Ordering with SQL by nearest latitude & longitude coordinates (MySQL & SQLite)
View nearby-coordinates.sql
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.
FROM table
ORDER BY ((lat-$user_lat)*(lat-$user_lat)) + ((lng - $user_lng)*(lng - $user_lng)) ASC