Order table by proximity to specific latitude/longitude (using MySQL+PHP)

一曲冷凌霜 提交于 2019-12-04 11:26:53

问题


MySQL (table)

id | url |    lat    |    lng
----------------------------------
1  |  x  | 44.339565 | -101.337891
----------------------------------
2  |  y  | 44.150681 | -101.074219
----------------------------------
3  |  z  | 43.897892 | -100.634766

what I want to do now is order the list according to their proximity to (43.834527,-99.140625).

$a = mysql_query("SELECT * FROM table ORDER BY proximity DESC");
while($b = mysql_fetch_assoc($a))
{
echo $b['url'].'<br />';
}

回答1:


You may be interested in checking out the following presentation:

  • Geo/Spatial Search with MySQL1 by Alexander Rubin

The author describes how you can use the Haversine Formula in MySQL to order by proximity and limit your searches to a defined range. He also describes how to avoid a full table scan for such queries, using traditional indexes on the latitude and longitude columns.


1 PDF Version



来源:https://stackoverflow.com/questions/3654554/order-table-by-proximity-to-specific-latitude-longitude-using-mysqlphp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!