我正在进行地理/空间搜索,我正在寻找附近的点。我有一个针对表运行的Haversine查询:
SELECT
uid, adrLat, adrLng,
round(3956 * 2 * ASIN(SQRT(POWER(SIN((39.97780609 - abs(adrLat)) * pi() / 180 / 2), 2) + COS(39.97780609 * pi() / 180) * COS(abs(adrLat) * pi() / 180) * POWER(SIN((-105.25861359 - adrLng) * pi() / 180 / 2), 2))), 2) AS distance
FROM dataPoints
WHERE adrLng BETWEEN -105.2680699902 AND -105.2491571898
AND adrLat BETWEEN 39.970559713188 AND 39.985052466812
HAVING distance <= 0.30 and distance > 0.00
ORDER BY distance;
这会给我一个像这样的结果:
+-----+-------------+---------------+----------+
| uid | adrLat | adrLng | distance |
+-----+-------------+---------------+----------+
| 191 | 39.97764587 | -105.25627136 | 0.12 |
| 520 | 39.97746658 | -105.25627136 | 0.13 |
| 265 | 39.97560120 | -105.25814056 | 0.15 |
| 266 | 39.97560120 | -105.25814056 | 0.15 |
| 274 | 39.97710037 | -105.25589752 | 0.15 |
| 98 | 39.97764969 | -105.26172638 | 0.17 |
| 576 | 39.97967911 | -105.25613403 | 0.18 |
| 575 | 39.97967911 | -105.25613403 | 0.18 |
| 469 | 39.97895813 | -105.25386810 | 0.26 |
| 470 | 39.97895813 | -105.25386810 | 0.26 |
| 1 | 39.98003006 | -105.25471497 | 0.26 |
| 383 | 39.97621155 | -105.26350403 | 0.28 |
| 431 | 39.97459793 | -105.25507355 | 0.29 |
| 430 | 39.97459793 | -105.25507355 | 0.29 |
| 429 | 39.97459793 | -105.25507355 | 0.29 |
| 428 | 39.97459793 | -105.25507355 | 0.29 |
+-----+-------------+---------------+----------+
然而,正如你可能知道的那样,一些记录在表中是重复的(这就是数据提供给我的方式,我必须保留它。)265:266,576:575,469:470 ,和431-428都是重复的。
有没有办法修改查询以仅选择唯一记录?看起来我必须匹配adrLat和adrLng来过滤重复项,但我不确定我是否可以在同一个查询中完成所有操作,或者我是否必须对结果进行一些后期处理。
答案 0 :(得分:3)
SELECT DISTINCT colum_name FROM table
SELECT关键字允许我们从表中的一列(或多列)中获取所有信息。当然,这必然意味着会有裁员。如果我们只想选择每个DISTINCT元素怎么办?这在SQL中很容易实现。我们需要做的就是在SELECT之后添加DISTINCT。语法如下:
答案 1 :(得分:3)
SELECT adrLat, adrLng,
round(3956 * 2 * ASIN(SQRT(POWER(SIN((39.97780609 - abs(adrLat)) * pi() / 180 / 2), 2) + COS(39.97780609 * pi() / 180) * COS(abs(adrLat) * pi() / 180) * POWER(SIN((-105.25861359 - adrLng) * pi() / 180 / 2), 2))), 2) AS distance
FROM mytable
WHERE adrLng BETWEEN -105.2680699902 AND -105.2491571898
AND adrLat BETWEEN 39.970559713188 AND 39.985052466812
GROUP BY
adrLat, adrLng
HAVING distance <= 0.30
AND distance > 0.00
ORDER BY
distance
答案 2 :(得分:0)
我仍然需要返回所有四列
你已经得到那里的唯一数据(例如,uid 576和575返回相同的坐标 - 但是uid明显不同)。
你对'独特'的定义明显不同于我们 - 你能提供一个你期望看到的例子吗?