MYSQL求经纬度之间的距离

CREATE DEFINER=`root`@`%` FUNCTION `getDistance`(`lat1` decimal(10,7),`lon1` decimal(10,7),`lat2` decimal(10,7),`lon2` decimal(10,7)) RETURNS decimal(10,2)
BEGIN
    #Routine body goes here...
 DECLARE dis double;
    SET dis = acos(sin(lat1 * 3.1415 / 180) * sin(lat2 * 3.1415 / 180) + cos(lat1 * 3.1415 / 180) * cos(lat2 * 3.1415 / 180) * cos(lon1 * 3.1415 / 180 - lon2 * 3.1415 / 180)) * 6378.138;
    RETURN dis;
END

注意:

1.创建函数用户的有权限,没权限无法创建

2.参数小数点后位数指定,否则经纬度相近距离为0

你优秀了,自然有对的人与你并肩,错的人迟早走散,而相爱的终将再相逢。
原文地址:https://www.cnblogs.com/szrhsd/p/14179916.html