给定一个包含几何数据的多边形表并使用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;
返回
hi hello peanut butter hello hi butter peanut
我想要
hi hello peanut butter
答案 0 :(得分: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;
答案 1 :(得分: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;