循环中MySQL中两个POINT值之间的距离

时间:2017-01-09 10:11:34

标签: mysql geometry

我将几何数据存储在MySQL数据库中

--------------------------------------------------
| ID | SPATIAL_INDEX | COORDS         | DISTANCE  |
| 1  | ...           | POINT(0.5,0.5) |     0     |
| 2  | ...           | POINT(0.7,0.7) |     ?     |
---------------------------------------------------

coords来自2D-Cartesian跟踪系统。 如何用一行的坐标和最后一行之间的距离填充DISTANCE列?

Row 1: Distance = 0;
Row 2: Distance = Distance between coords of row 1 and row 2
Row 3: Distance = Distance between coords of row 2 and row 3
[...]

1 个答案:

答案 0 :(得分:2)

SQL有一个特殊的空间函数来计算两点之间的距离。您可以使用它加上表本身的连接来实现您想要的效果。

UPDATE tablename AS a 
LEFT JOIN tablename AS b 
ON b.id = a.id-1 
SET a.distance = IFNULL(DISTANCE(a.coord,b.coord),0);