我有一个纬度\经度点数的DB。我试图得到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)将显示两次。
答案 0 :(得分: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
答案 1 :(得分: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 |
+------------+------------+