How to calculate distance using latitude and longitude?

我与影子孤独终老i 提交于 2019-12-01 10:54:10
SELECT ROUND(6371 * acos(cos(radians('lat')) * cos(radians(latitude)) * cos(radians(longitude) - radians('long')) + sin(radians('lat')) * sin(radians(latitude)))) as distance,latitude,longitude, from your_table HAVING distance<=20  order by distance

You can use a query similar to shown above in your SQL where 'lat' and 'long' are the variable values that you are passing to search for. The 'longitude' and 'latitude' are column names from your table. And btw, this is called Haversine formula and the constant 6371 is used to get distance in KM, while 3959 is used to get distance in miles. You can use either of them as per your requirement.

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