Mysql 根据经纬度计算距离

方式1:st_distance_sphere:没用除以1000,所以是以米为单位

SELECT *,st_distance_sphere(point(lng,lat),point(116.3424590000,40.0497810000)) as distance FROM school ORDER BY distance ASC

方式2:st_distance:st_distance 计算的结果单位是度,需要乘111195(地球半径6371000*PI/180)是将值转化为米。

SELECT *,(st_distance(point(lng,lat),point(116.3424590000,40.0497810000))*111195/1000 ) as distance FROM school ORDER BY distance ASC

方式3:

select *,

ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$lat}*PI()/180-lat*PI()/180)/2),2)+COS({$lat}*PI()/180)*COS(lat*PI()/180)*POW(SIN(({$lng}*PI()/180-lng*PI()/180)/2),2)))*1000) AS distance 

from school order by distance asc

你可能感兴趣的:(Mysql 根据经纬度计算距离)