查找附近——mysql解决方法

mysql 函数

DELIMITER $$
 
CREATE DEFINER=`root`@`%` FUNCTION `GETDISTANCE`(lat1 DOUBLE, lng1 DOUBLE, lat2 DOUBLE, lng2 DOUBLE) RETURNS double
 
READS SQL DATA
 
DETERMINISTIC
 
BEGIN
 
DECLARE RAD DOUBLE;
 
DECLARE EARTH_RADIUS DOUBLE DEFAULT 6378137;
 
DECLARE radLat1 DOUBLE;
 
DECLARE radLat2 DOUBLE;
 
DECLARE radLng1 DOUBLE;
 
DECLARE radLng2 DOUBLE;
 
DECLARE s DOUBLE;
 
SET RAD = PI() / 180.0;
 
SET radLat1 = lat1 * RAD;
 
SET radLat2 = lat2 * RAD;
 
SET radLng1 = lng1 * RAD;
 
SET radLng2 = lng2 * RAD;
 
SET s = ACOS(COS(radLat1)*COS(radLat2)*COS(radLng1-radLng2)+SIN(radLat1)*SIN(radLat2))*EARTH_RADIUS;
 
SET s = ROUND(s * 10000) / 10000;
 
RETURN s;
 
END$$
 
DELIMITER ;
sql  查询

SELECT *,lat,lon,GETDISTANCE(lat,lon,30.663262,104.071619) AS distance FROM  c_shop WHERE 1 HAVING distance<1000 ORDER BY distance ASC LIMIT 0,10



你可能感兴趣的:(查找附近——mysql解决方法)