sql - calculate distance live and order asc|desc -
so when start web app, server gets user geolocation , save variable (latitude/longitude) i've got database with: id, name, latitude, longitude
now want select entries database , order them distance
i know how calculate distance between coordinates, dont know how order things "live"
can guys give me hint how solve this?
you can specify distance calculation (say example (longitude + latitude) divided 42) in order by
clause
select id, name, latitude, longitude, (longitude + latitude) / 42 distance yourtable order (longitude + latitude) / 42
however, because distance calculation more complex (longitude + latitude) / 42, might want consider ordering results based on ordinal of column in result set (i think databases support this). like.....
select id, name, latitude, longitude, /* insert distance calculation here */ distance yourtable order 5 -- column ordinals 1-based
you should note though there gotchas approach. example, if order of columns changed or columns added, order by
ordinal must changed or see unexpected results. if order field name or calculation isn't issue.
Comments
Post a Comment