我正在使用 Postgresql 9.3 并且我安装了 cube 和 earthdistance 扩展。
我正在尝试关注 this tutorial ,所以我有一个包含 4 个字段的简单事件表:id、name、lat、lng。
现在我正在尝试运行此查询,以获取 1 公里半径内的所有事件:
SELECT events.id, events.name FROM events WHERE earth_box(31.789225, 34.789612, 1000) @> ll_to_earth(events.lat, events.lng);
但我一直收到这个错误:
20:59:34 Kernel error: ERROR: function earth_box(numeric, numeric, integer) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
所以我用类型转换做了同样的事情:
SELECT events.id, events.name FROM events WHERE earth_box(CAST(31.789225 AS float8), CAST(34.789612 AS float8), 1000) @> ll_to_earth(events.lat, events.lng);
我得到:
21:16:17 Kernel error: ERROR: function earth_box(double precision, double precision, integer) does not exist
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
最佳答案
好的,几个小时后我终于明白了:)
显然我使用的教程已经过时,正确的语法是这样的:
SELECT events.id, events.name FROM events WHERE earth_box(ll_to_earth(31.789225,34.789612), 1000) @> ll_to_earth(events.lat, events.lng);
关于PostgreSQL:如何获得一定半径内的所有点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26147954/