0

Poly1 是一个边界框,用于计算 f1 和 f2 在其边界内的面积。如果 f1 存在,但 f2 不存在,如何使此查询返回结果?现在,如果边界框内只有 f1,则查询不会返回任何结果。

SELECT ST_Transform(poly1.the_geom,3857) AS the_geom_webmercator, 
       ST_AREA(ST_Union(ST_Transform(ST_Intersection(f1.the_geom,poly1.the_geom),3857)))*.000247105381 AS acreage_of_1,
       ST_AREA(ST_Union(ST_Transform(ST_Intersection(f2.the_geom,poly1.the_geom),3857)))*.000247105381 AS acreage_of_2
            FROM poly1 JOIN farmland f1 ON f1.polygon_type = 'A' AND
                                           st_intersects(f1.the_geom,poly1.the_geom)
                       JOIN farmland f2 ON f2.polygon_type = 'B'
            GROUP BY poly1.the_geom
4

1 回答 1

1

您希望在“farmland f2”语句之前有一个左外连接。左外连接将保留所有行,即使 f2 中没有匹配项:

Select ST_Transform(poly1.the_geom,3857) as the_geom_webmercator, 
       ST_AREA(ST_Union(ST_Transform(ST_Intersection(f1.the_geom,poly1.the_geom),3857)))*.000247105381 AS acreage_of_1,
       ST_AREA(ST_Union(ST_Transform(ST_Intersection(f2.the_geom,poly1.the_geom),3857)))*.000247105381 AS acreage_of_2
FROM poly1 JOIN
     farmland f1
     ON f1.polygon_type = 'A' AND st_intersects(fp.the_geom,poly1.the_geom) **LEFT OUTER** JOIN
     farmland f2
     ON f2.polygon_type = 'B'
 group by poly1.the_geom
于 2012-07-16T19:40:52.973 回答