在SQL查询中动态计算距离

时间:2017-07-13 20:10:39

标签: mysql mariadb geospatial

我一直在寻找关于如何动态计算并将距离返回到MySQL / Mariadb表中每行提供的GPS坐标(使用地理空间扩展)的答案。

我的查询如下:(我使用任意GPS坐标作为占位符,而不是动态生成所提供坐标的查询)。

SET @distance = 0;
select name, X(location), Y(location), 
@distance = (((ACOS(SIN( 17.4681194 * PI() / 180) 
* SIN(X(location) * PI() / 180) + COS(17.4681194 * PI() / 180) 
* COS(X(location) * PI() / 180) * COS((78.495423 - Y(location)) 
* PI() / 180)) * 180 / PI()) * 60 * 1.1515)
* 1.609) 
as distance from Points

问题是在我的输出中,所有距离都是0或1.我小心地在Points表中播种,所以我知道这些值是错误的。

我在SQL中而不是在Python中(或在代码级别)执行此操作的原因是因为我希望能够使用ORDER BY和TOP一旦我完成距离并获取3距离桌子最近的位置。

我的输出如下:

+----------------+--------------------+-------------------+----------+
| name           | X(location)        | Y(location)       | distance |
+----------------+--------------------+-------------------+----------+
| randomsr1      |         17.4901059 |        78.4819368 |        0 |
| randomsr2      |         17.4895687 |        78.4830526 |        0 |
| randomsr3      |           17.45602 |         78.448551 |        0 |
| randomsr4      |         17.4681194 |         78.495423 |        1 |
| randomsra      |          17.515589 |        78.4749738 |        0 |
| randomsrb      |         17.4041657 |        78.4930975 |        0 |
| randomsrc      |  17.40658541008292 | 78.47815974049286 |        0 |
| randomsrd      | 17.468381171457448 | 78.46161846857831 |        0 |
| randomsr6      |   17.4649552904376 | 78.45982138853833 |        0 |
| location5      |          17.515562 |         78.474973 |        0 |
+----------------+--------------------+-------------------+----------+

1 个答案:

答案 0 :(得分:0)

对不起,愚蠢的问题。在为计算变量赋值时,我错过了' = '之前的''。

select name, X(location), Y(location), 
@distance := (((ACOS(SIN( 17.4681194 * PI() / 180) 
* SIN(X(location) * PI() / 180) + COS(17.4681194 * PI() / 180) 
* COS(X(location) * PI() / 180) * COS((78.495423 - Y(location)) 
* PI() / 180)) * 180 / PI()) * 60 * 1.1515)
* 1.609) 
as distance from Points;

现在返回:

+----------------+--------------------+-------------------+--------------------+
| name           | X(location)        | Y(location)       | distance           |
+----------------+--------------------+-------------------+--------------------+
| randomsr1      |         17.4901059 |        78.4819368 |  2.831730948299528 |
| randomsr2      |         17.4895687 |        78.4830526 | 2.7213934426686652 |
| randomsr3      |           17.45602 |         78.448551 |  5.149213292389459 |
| randomsr4      |         17.4681194 |         78.495423 |                  0 |
| randomsra      |          17.515589 |        78.4749738 |  5.705042143392742 |
| randomsrb      |         17.4041657 |        78.4930975 | 7.1137416855834115 |
| randomsrc      |  17.40658541008292 | 78.47815974049286 |  7.081262494869047 |
| randomsrd      | 17.468381171457448 | 78.46161846857831 | 3.5847244189457905 |
| randomsr6      |   17.4649552904376 | 78.45982138853833 | 3.7915534696376625 |
| location5      |          17.515562 |         78.474973 |  5.702298292174848 |
+----------------+--------------------+-------------------+--------------------+