我在Postgres DB中有一个表,其中包含地点及其对应的纬度和经度值。
Places (id, name, lat, lng) # primary-key(id)
我将得到一对形成矩形的纬度和经度的输入。
{
"long_ne": 12.34,
"lat_ne": 34.45,
"long_sw": 15.34,
"lat_sw": 35.56
}
我想获取所有落在矩形内的行。
无法根据行的lat-lng值对行进行排序,因为这会在插入新值时引起麻烦。
解决此问题以优化查询以获得结果的最佳方法是什么?
这是你想要的吗?
Where
:lat_no
,:lat_ws
:long_no
and:long_sw
are the input parameters to the query.This gives you all rows whose latitude and longitude fall between the square boundaries. Note that
between
considers the inteval inclusive of their bounds on both ends. You can change this as needed with inequality conditions, for example, this makes the match inclusive on the lower bound and exclusive on the upper bound:一个简单的SQL查询就足够了吗?