mysql空间数据相关操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql空间数据相关操作相关的知识,希望对你有一定的参考价值。
建表语句:
CREATE TABLE ts.points ( name VARCHAR (20) NOT NULL, location POINT NOT NULL, description VARCHAR (200) );
添加记录如下:
INSERT INTO Points (name, location) VALUES ( ‘p1‘ , GeomFromText( ‘ POINT(10 20) ‘ ) ) INSERT INTO Points (name, location) VALUES ( ‘p2‘ , GeomFromText( ‘ POINT(11 30) ‘ ) ) INSERT INTO Points (name, location) VALUES ( ‘p3‘ , GeomFromText( ‘ POINT(10 21) ‘ ) )
查询操作如下:
SELECT NAME, ASTEXT(LOCATION),X(LOCATION),Y(LOCATION) FROM POINTS
查询所有记录离指定点的距离
SELECT *,ROUND(6378.138*2*ASIN(SQRT(POW(SIN( (X(LOCATION)*PI()/180-10*PI()/180)/2),2)+COS(X(LOCATION)*PI()/180)*COS(10*PI()/180)* POW(SIN( (Y(LOCATION)*PI()/180-20*PI()/180)/2),2)))*1000) AS DISTANCE FROM POINTS ORDER BY DISTANCE ASC
注:指定点信息(10,20),ps:10为X(经度),20为Y(纬度)
查询附近100公里数据:
SELECT name ,X(LOCATION),Y(LOCATION) FROM points WHERE MBRContains ( LineString ( Point ( 10 + 100 / ( 111.1 / COS(RADIANS(104.067221))), 20 + 100 / 111.1 ), Point ( 10 - 100 / ( 111.1 / COS(RADIANS(104.067221))), 20 - 100 / 111.1 ) ), location )
目标点坐标为(10,20)
以上是关于mysql空间数据相关操作的主要内容,如果未能解决你的问题,请参考以下文章