3

给定一个包含几何数据并使用 ST_INTERESECTS() 的多边形表,我们如何返回相交的多边形,同时防止我们的结果冗余?

需要明确的是,如果我们返回多边形 A 与多边形 B 相交,那么返回行多边形 B 与多边形 A 相交也是多余的。

例子:

SELECT table1.name, table2.name 
FROM tl AS table1, tl AS table2
WHERE table1.name <> table2.name  // prevent returning A intersects A
AND ST_INTERSECTS( table1.coords, table2.coords) 
ORDER BY table1.name asc;

返回

你好  
花生酱   
你好  
黄油花生

而且我要

你好  
花生酱
4

2 回答 2

4

您可以决定总是先呈现较小的值,并使用<而不是仅仅<>

SELECT table1.name, table2.name 
FROM tl AS table1, tl AS table2
WHERE table1.name < table2.name -- The aforementioned assumption
AND ST_INTERSECTS( table1.coords, table2.coords) 
ORDER BY table1.name asc;
于 2013-11-01T17:21:26.503 回答
1

另一种选择是对结果应用 DISTINCT:

SELECT DISTINCT least(table1.name, table2.name) as name1, 
                greatest(table1.name, table2.name) as name2
FROM tl AS table1, tl AS table2
WHERE table1.name <> table2.name  -- prevent returning A intersects A
AND ST_INTERSECTS( table1.coords, table2.coords) 
ORDER BY table1.name asc;
于 2013-11-01T17:24:34.627 回答