微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;


搜索结果

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐