1. 程式人生 > >mysql 通過經緯度查詢附近的地點位置

mysql 通過經緯度查詢附近的地點位置

谷歌的方案

語句:

SELECT  
  id, `name`,(  
    6371 * acos (  
      cos ( radians(60.000000) )  
      * cos( radians( lat ) )  
      * cos( radians( lon ) - radians(70.000000) )  
      + sin ( radians(60.000000) )  
      * sin( radians( lat ) )  
    )  
  ) AS distance  
FROM t_demo
HAVING distance < 2  
ORDER BY distance 
LIMIT 0 , 20;  

說明:當前位置緯度:60.000000 經度:70.000000  以公里代替里程搜尋,用6371替換3959。

英文原文:

The SQL statement that will find the closest 20 locations that are within a radius of 30 miles to the 78.3232, 65.3234 coordinate. It calculates the distance based on the latitude/longitude of that row and the target latitude/longitude, and then asks for only rows where the distance value is less than 30 miles, orders the whole query by distance, and limits it to 20 results. To search by kilometers instead of miles, replace 3959 with 6371.