CREATE FUNCTION `lat_lng_distance` (lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT) RETURNS FLOAT DETERMINISTIC BEGIN RETURN ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN((lat1 * PI() / 180 - lat2 * PI() / 180) / 2), 2) + COS(lat1 * PI() / 180) * COS(lat2 * PI() / 180) * POW(SIN(( lon1 * PI() / 180 - lon2 * PI() / 180 ) / 2),2))),2); END
创建完成,然后再在sql语句中调用
$shops = Db::name('XXXX') ->field('id,lat_lng_distance('.$lat.','.$lon.',lat,lon) as distance') ->where($where) ->page($page,10) ->order('distance asc') ->select()->toArray();
本文来自博客园,作者:三哥-Noble,转载请注明原文链接:https://www.cnblogs.com/Noble-zeng/p/13560836.html