PostgreSQL: How to get all points in certain radius

时光怂恿深爱的人放手 提交于 2020-01-02 12:26:12

问题


I'm using Postgresql 9.3 and iv'e installed cube and earthdistance extensions.

I'm trying to follow this tutorial, so i have a simple events table with 4 fields: id, name, lat, lng.

Now i'm trying to run this query, to get all events within 1 km radius:

SELECT events.id, events.name FROM events WHERE earth_box(31.789225, 34.789612, 1000) @> ll_to_earth(events.lat, events.lng);

but i keep getting this error:

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.

So i ran the same thing with casting:

  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);

and i get :

   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.

回答1:


O.K, finally after couple of hours i got it :)

Apparently the tutorial i was working with was outdated, the correct syntax is this:

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); 


来源:https://stackoverflow.com/questions/26147954/postgresql-how-to-get-all-points-in-certain-radius

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!