ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

mysql 空间函数

2026/9/24 18:53:15 拓冰建站 浏览量
mysql 空间函数

ST_GeomFromText:将文本表示的几何对象转换为几何对象。

SELECT ST_GeomFromText('POINT(1 1)');

ST_AsText:将几何对象转换为文本表示。

SELECT ST_AsText(ST_GeomFromText('POINT(1 1)'));

ST_Contains:判断一个几何对象是否包含另一个几何对象。

SELECT ST_Contains(ST_GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))'), ST_GeomFromText('POINT(1 1)'));

ST_Distance:计算两个几何对象之间的距离。

SELECT ST_Distance(ST_GeomFromText('POINT(1 1)'), ST_GeomFromText('POINT(2 2)'));

ST_Intersection:计算两个几何对象的交集。

SELECT ST_Intersection(ST_GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))'), ST_GeomFromText('POLYGON((1 1, 1 3, 3 3, 3 1, 1 1))'));

ST_Buffer:根据给定的半径创建一个缓冲区。

SELECT ST_Buffer(ST_GeomFromText('POINT(1 1)'), 1);

ST_DISTANCE_SPHERE: 计算给定的坐标点到目标的坐标点距离

select ST_DISTANCE_SPHERE(position,POINT(#{longitude} , #{latitude})) distance 

ST_Within: 判断一个点是否在由坐标点组成的几何内

select ST_Within(GeomFromText('POINT(${longitude} ${latitude})'), points) ;

JAVA代码处理坐标点信息:

obj.setPosition("POINT(" + longitude + " " + latitude + ")");mapper.xml 需要将坐标转换为point类型的文本
st_PointFromText(#{position})同样修改的时候:
update table_name set position = st_PointFromText(#{position})查询将point类型的数据转换为文本或获取经纬度select   AsText(position) position,ST_X(position) longitude,ST_Y(position) latitudefrom table_name根据距离排序查询:selectST_X(position) longitude,ST_Y(position) latitude,ST_DISTANCE_SPHERE(position,POINT(#{longitude} , #{latitude})) distancefrom table_namewhere #{maxRadius} >= ST_DISTANCE_SPHERE(position,POINT(#{longitude} , #{latitude}))order by distance asc