/*传入的参数为 纬度 纬度 经度 ASC升序由近至远 DESC 降序 由远到近 */
SELECT
*,
ROUND(
6378.138 * 2 * ASIN(
SQRT(
POW(
SIN(
(
40.0497810000 * PI() / 180 - lat * PI() / 180
) / 2
),
2
) + COS(40.0497810000 * PI() / 180) * COS(lat * PI() / 180) * POW(
SIN(
(
116.3424590000 * PI() / 180 - lon * PI() / 180
) / 2
),
2
)
)
) * 1000
) AS juli
FROM
customer
ORDER BY
juli ASC
lat, lon为经纬度
php代码
//根据经纬度计算距离 其中A($lat1,$lng1)、B($lat2,$lng2) function getDistance($lat1,$lng1,$lat2,$lng2) { //地球半径 $R = 6378137; //将角度转为狐度 $radLat1 = deg2rad($lat1); $radLat2 = deg2rad($lat2); $radLng1 = deg2rad($lng1); $radLng2 = deg2rad($lng2); //结果 $s = acos(cos($radLat1)*cos($radLat2)*cos($radLng1-$radLng2)+sin($radLat1)*sin($radLat2))*$R; //精度 $s = round($s* 10000)/10000; return round($s); }