1

我有一个纬度\经度点的数据库。我试图获得按距离排序的 DISTINCT 舍入结果。我的查询如下:

SELECT
   DISTINCT ROUND(`lat`,2), ROUND(`lon`,2),
   ( 3959 * acos( cos( radians(LAT_HERE) )
          * cos( radians( `lat` ) )
          * cos( radians( `lon` ) - radians(LONG_HERE) )
          + sin( radians(LAT_HERE) )
          * sin( radians( `lat` ) ) ) ) AS distance
FROM `user_data`
HAVING distance <= 99
ORDER BY distance
LIMIT 100

独特的是在回合前运行。例如 (29.333,29.334), (29.331,29.332) 将显示两次。

4

2 回答 2

1

尝试使用GROUP BY

    SELECT  ROUND(`lat`,2), ROUND(`lon`,2),
                   ( 3959 * acos( cos( radians(LAT_HERE) )
                   * cos( radians( `lat` ) )
                   * cos( radians( `lon` ) - radians(LONG_HERE) )
                   + sin( radians(LAT_HERE) )
                   * sin( radians( `lat` ) ) ) ) AS distance
                FROM `user_data`
                GROUP BY ROUND(`lat`,2) 
                HAVING distance <= 99
                ORDER BY distance
                LIMIT 100
于 2013-03-11T19:30:01.187 回答
0

其实你是不对的。DISTINCT 在先前评估的结果上运行。运行例如:

select distinct round(a,2), round(b,2) 
from ((select 29.333 as a,29.334 as b) union (select 29.331,29.332)) t3;

结果是:

+------------+------------+
| round(a,2) | round(b,2) |
+------------+------------+
|      29.33 |      29.33 |
+------------+------------+
于 2013-03-11T19:25:31.150 回答