2014-08-27 22 views
2

我有2个独立表格(链接&部分)。链接表内的信息是有点像这样,在地理区域上加入表格

Links Table 
---------------------------------------------------------------------------- 
| id | Description | startLat | startLng | endLat | endLng | 
---------------------------------------------------------------------------- 
| 1000259 | Link Number 1  |52.891372 |-1.768254 |52.892545 |-1.775278 | 
| 1000359 | Link Number 2  |52.894892 |-1.780513 |52.894306 |-1.774793 | 
| 1000279 | Link Number 3  |52.894306 |-1.774793 |52.895000 |-1.765273 | 
| 1000258 | Link Number 4  |52.895000 |-1.765273 |52.895500 |-1.755278 | 
| 1000255 | Link Number 5  |52.895500 |-1.755278 |52.896500 |-1.745278 | 
| 1000555 | Link Number 6  |52.896500 |-1.745278 |52.897250 |-1.735278 | 
---------------------------------------------------------------------------- 

并且这些部分表出现这样的,

Sections Table 
----------------------------------------------------------------------------- 
| id | Description | fromLat | fromLng | toLat | toLng | 
----------------------------------------------------------------------------- 
| 625  | Section 1  | 52.893598 | -1.775120 | 52.885053|-1.756409 | 
| 713  | Section 2  | 52.897273 | -1.788324 | 52.898285|-1.724721 | 
| ...  | ...   | ...  | ...  | ...  | ...  | 
| ...  | ...   | ...  | ...  | ...  | ...  | 
----------------------------------------------------------------------------- 

我想运行一个查询,给了我所有这一切都被部分覆盖的链接。因此,如果我说第2部分有多少链接,我应该收到部分信息经纬度所涵盖的所有链接。

P.S.请注意,部分比链接长...任何帮助!

+0

段和链接应该在同一行上吗?或者您如何知道该段上/段内的链接? – Bulat 2014-08-27 16:41:20

回答

2

你用join这样做,只是条件是不平等而不是平等。目前还不清楚您是要部分覆盖还是覆盖全部。以下是部分覆盖的示例:

select s.*, l.* 
from sections s join 
    links l 
    on (l.startlat < s.tolat and l.endlast > s.fromlat) and 
     (l.startlong < s.tolong and l.endlong > s.fromlong); 
+2

这会起作用,但如果你使用Postgres,你应该考虑使用Postgis及其相关的空间函数。您可以使用空间索引(R-tree)和像ST_Intersects,ST_Contains这样的函数,这些函数在一些带有B树的数字字段上的性能将明显优于一系列< and >运算符。将4列转换为2(经纬度)点会有一些开销,但对于任何重要的表格大小,这种努力都是值得的。 – 2014-08-27 17:09:43

+0

这是我回答有点类似的问题。 http://stackoverflow.com/questions/24212355/query-by-coordinates-takes-too-long-options-to-optimize/24218861#24218861 – 2014-08-27 17:13:14

+0

@JohnBarça:对于简单的情况(小区域可以视为平面) ,没有Postgis你可以做很多事情。框或圆圈上的GiST索引支持[包含运算符'<@'](http://www.postgresql.org/docs/current/interactive/functions-geometry.html#FUNCTIONS-GEOMETRY-OP-TABLE)。 – 2014-08-27 23:56:42