我有一个包含位置的mysql表,例如:
ID latitude longitude value
1 11.11111 22.22222 1
2 33.33333 44.44444 2
3 11.11112 22.22223 5我希望选择彼此靠近的记录(在上面的示例中,第1行和第3行),这样我就可以将它们作为一个记录插入到一个新表中。说到近处,比方说100米吧。我希望新的桌子是:
ID latitude longitude value
1 11.11111 22.22222 3
2 33.33333 44.44444 2正如您所看到的,我希望保留第一个记录的坐标,并在列'value‘中插入两个记录的平均值。
我使用的距离公式如下:
R*SQRT(((lon2*c-lon1*c)*cos(0.5*(lat2*c+lat1*c)))^2 + (lat2*c-lat1*c)^2)哪里
[lat1, lon1] the coordinates of the first location,
[lat2, lon2] the coordinates of the second location,
[R] the average radius of Earth in meters,
[c] a number to convert degrees to radians.这个公式适用于短距离。
因此,我的问题不是将lat、lon转换为距离,而是转换我的SQL。我知道如何选择记录的最大距离为100米,从特定的lat,龙坐标,但我不知道如何选择一个最大的距离与彼此的记录。
我使用-and的一种方法是在PHP中一个一个地循环记录,并对每个记录进行一个查询,以选择接近的记录。但是这样,我在循环中执行SQL查询,据我所知,这是一种糟糕的做法,特别是如果记录将达到数千条。
我希望我说的很清楚。如果没有,我很乐意向你提供更多的信息。
谢谢你的帮助。
发布于 2017-04-27 20:33:37
下面是一个SQL,用于获取范围内的所有位置:
SELECT
ID,
latitude,
longitude,
(6371 * acos (cos( radians(origin.latitude)) * cos( radians( destination.latitude ))
* cos( radians(destination.longitude) - radians(origin.longitude)) + sin(radians(origin.latitude))
* sin( radians(destination.latitude)))) AS distance
FROM myTable as destination, myTable as origin
WHERE destination.id = myId
HAVING distance < 100 --some value in kilometers6371是公里的常数。3959是英里的常数。
https://stackoverflow.com/questions/43667187
复制相似问题