mysql 5.6.1 加入了空間數據支持功能,新增了st_*相關函數,可以非常方便的計算兩個地理坐標點的距離了。
如下例子:按我的坐標計算周邊坐標的距離並由近到遠排序
select name,st_distance(point(113.327955,23.129717),point)*111195 as distance,address from table1 where st_distance(point(113.327955,23.129717),point)*111195 < 100 order by distance asc limit 100
注意:其中point字段類型是 point,其值可以通過以下方法寫入:
update table1 set point = point(113.123232,24.1324234)
如下例子:按我的坐標計算周邊坐標的距離並由近到遠排序
select name,st_distance(point(113.327955,23.129717),point)*111195 as distance,address from table1 where st_distance(point(113.327955,23.129717),point)*111195 < 100 order by distance asc limit 100
注意:其中point字段類型是 point,其值可以通過以下方法寫入:
update table1 set point = point(113.123232,24.1324234)
st_distance 計算的結果單位是 度,需要乘111195(地球半徑6371000*PI/180) 是將值轉化為米
其它相關空間函數參考官方手冊
http://dev.mysql.com/doc/refman/5.6/en/spatial-relation-functions-mbr.html