MySQL Great Circle Distance (Haversine formula)

前端 未结 9 1365
栀梦
栀梦 2020-11-21 06:53

I\'ve got a working PHP script that gets Longitude and Latitude values and then inputs them into a MySQL query. I\'d like to make it solely MySQL. Here\'s my current PHP Cod

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 07:08

     SELECT *, (  
        6371 * acos(cos(radians(search_lat)) * cos(radians(lat) ) *   
    cos(radians(lng) - radians(search_lng)) + sin(radians(search_lat)) *         sin(radians(lat)))  
    ) AS distance  
    FROM table  
    WHERE lat != search_lat AND lng != search_lng AND distance < 25  
     ORDER BY distance  
    FETCH 10 ONLY 
    

    for distance of 25 km

提交回复
热议问题