0

如何以 CDBCriteria 格式使用此查询 >>

SET @orig_lat=122.4058;

SET @orig_lon=37.7907;

SET @dist=10;

SELECT *
     , 3956 
     * 2 
     * ASIN(SQRT(POWER(SIN((@orig_lat - abs(dest.latitude)) * pi()/180 / 2),2) + COS(@orig_lat * pi()/180) * COS(ABS (dest.latitude) * pi()/180) * POWER(SIN((dest.longitude - @orig_lon) * pi()/180 / 2), 2))) distance
  FROM business 
HAVING distance < @dist
 ORDER 
    BY distance 
 LIMIT 10;

有人在这里问。但我没有得到我的答案。

4

1 回答 1

0

考虑将“Haversine”公式放入 MySQL 函数中以开始:

CREATE FUNCTION `HAVERSINE`(origin_lat FLOAT, origin_long FLOAT, dest_lat FLOAT, dest_long FLOAT) 
RETURNS float 
DETERMINISTIC 
BEGIN 
    DECLARE R FLOAT;
    SET R = 3956 * 2 * ASIN(SQRT(POWER(SIN((origin_lat - dest_lat) * pi()/180 / 2), 2) + COS(origin_lat * pi()/180) * COS(dest_lat * pi()/180) * POWER(SIN((origin_long-dest_long) * pi()/180 / 2), 2))) * 1.609344; 
    RETURN R; 
END;

这样您就可以在查询中调用它。

然后您可以通过创建存储过程来进一步抽象以获取最近的业务。就像是:

CREATE PROCEDURE `findClosestBusinessesToLatLng`(lat FLOAT, lng FLOAT)
BEGIN
    SELECT   *, HAVERSINE(lat, lng, businesses.lat, businesses.lng) AS dist
    FROM     businesses
    ORDER BY dist
END

然后你的查询就变成了

Call(findClosestBusinessesToLatLng(122.4058,37.7907));
于 2013-06-15T12:42:42.330 回答