MySQL 5.7 以后新增了空间函数 ST_Distance_Sphere(g1, g2 [, radius])

返回坐标 g1 和 g2 在球面上的最短距离,单位是米。半径 radius 默认值为 6370986 米,即地球半径。

使用举例:

mysql> SELECT ST_Distance_Sphere(Point(108.321712, 22.833), Point(109.625045, 23.100797)) AS d1;
+--------------------+
| d1                 |
+--------------------+
| 136717.27885025347 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT Round(ST_Distance_Sphere(Point(108.321712, 22.833), Point(109.625045, 23.100797)) / 1000, 1) AS d2;
+-------+
| d2    |
+-------+
| 136.7 |
+-------+
1 row in set (0.00 sec)

mysql>

d1 值单位是米(m),d2 值单位是千米(km)。