我有postgresql列
geography(Point,4326)我在它中插入了一些行
POINT(LONG LAT)该数据已成功插入,并且我可以毫无问题地检索它,现在我希望使用以下查询将最近的条目获取到特定的点
SELECT "cafes".* FROM "cafes" ORDER BY "latlng" <-> (SELECT latlng FROM cafes WHERE id = '3') LIMIT 1但是我得到了以下错误
ERROR: operator does not exist: geography <-> geography
LINE 1: ...es".* FROM "cafes" ORDER BY "latlng" <-> (SELEC...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.发布于 2015-07-28 23:14:39
<->“距离”运算符适用于PostgreSQL几何数据类型,而不是PostGIS geography数据类型。使用geography数据类型,您可以使用PostGIS函数ST_Distance()并找到最小值。
WITH this_cafe (latlng) AS (
SELECT latlng FROM cafes WHERE id = '3'
)
SELECT cafes.*, ST_Distance(cafes.latlng, this_cafe.latlng, 'false') AS dist
FROM cafes, this_cafe
ORDER BY dist ASC
LIMIT 1注意,函数的第三个参数useSpheroid被设置为false,这将使函数速度更快。这不太可能影响结果,因为咖啡馆往往相距较近。
这假设只有一个带id = 3的咖啡馆。如果可能有更多,那么限制CTE只返回1行。
https://stackoverflow.com/questions/31687514
复制相似问题