表格信息
ID | NAME | DIRECTION
--------------------------
1 | BUS 1 | departure
2 | BUS 1 | return
3 | BUS 2 | departure
4 | BUS 2 | return
5 | BUS 3 | departure
6 | BUS 3 | return
表COORDINATES
ID | INFORMATION_ID | LAT | LON |
-------------------------------------------------
1 | 1 | 19.171184 | -96.174029 |
2 | 1 | 19.171021 | -96.173991 |
3 | 1 | 19.171002 | -96.173979 |
5 | 2 | 19.193244 | -96.138732 |
6 | 2 | 19.193368 | -96.138957 |
7 | 2 | 19.193332 | -96.138741 |
我试图在500米范围内显示附近的公交线路,我实际上可以通过此查询进行此操作
SELECT DISTINCT NAME,
((example.lat - COORDINATES.LAT) * (example.lat - COORDINATES.LAT) + (example.lon - COORDINATES.LON) * (example.lon - COORDINATES.LON)) AS DISTANCE
FROM COORDINATES
INNER JOIN INFORMATION
ON COORDINATES.INFORMATION_ID=INFORMATION.ID
WHERE DISTANCE < 0.000023544 //supposedly 500 m, im not sure
ORDER BY DISTANCE
但问题是我在NAME字段中重复了值,因为我有几个来自该范围内的总线路径的latlon点, 我只需要使用相应的名称返回最小距离,如下所示:
ID | NAME | DISTANCE
---------------------
1 | BUS 1 | 1.349834...
3 | BUS 2 | 1.367033...
我希望找到解决此问题的任何指导,谢谢。
答案 0 :(得分:0)
看看这是否有助于缩短距离。
SELECT NAME, MIN(DISTANCE) FROM
(
SELECT NAME,
((19.171150 - COORDINATES.LAT) * (19.171184 - COORDINATES.LAT) +
(-96.173990 - COORDINATES.LON) * (-96.173990 - COORDINATES.LON)) AS DISTANCE
FROM COORDINATES
INNER JOIN INFORMATION
ON COORDINATES.INFORMATION_ID=INFORMATION.ID
WHERE DISTANCE < 0.000023544
)
GROUP BY NAME