postgresql 搜索指定距离内的记录 按近到远排序 并返回距离

前端之家收集整理的这篇文章主要介绍了postgresql 搜索指定距离内的记录 按近到远排序 并返回距离前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

脚本


CREATE TABLE mylocation ( 
  id SERIAL PRIMARY KEY,geom GEOMETRY(Point,4326),name VARCHAR(128),x double precision,y double precision
); 
 
INSERT INTO mylocation (geom,name,x,y) VALUES (
  ST_GeomFromText('POINT(0.0001 0)','zhangsan',0.0001,0
);
INSERT INTO mylocation (geom,y) VALUES (
  ST_GeomFromText('POINT(0.001 0)',0.001,y) VALUES (
  ST_GeomFromText('POINT(0.1 0)',0.1,0
);



SELECT id,geom,y,ST_DistanceSphere(
                      geom,ST_GeometryFromText('POINT(0 0)')) distance
FROM mylocation
WHERE ST_DWithin(
  geom,ST_GeomFromText('POINT(0 0)',0.001
)ORDER BY distance asc;;

查询语句 下面距离单位为m

SELECT id,ST_GeometryFromText('POINT(0 0)')) distance
FROM mylocation
WHERE ST_DWithin(
  geom::geography,4326)::geography,1000
) ORDER BY distance asc;


搜索结果

原文链接:https://www.f2er.com/postgresql/194258.html

猜你在找的Postgre SQL相关文章