将查询输出插入postgres中的另一个表?

时间:2016-04-26 09:19:52

标签: postgresql

我在Postgres 9.4工作。我有两张桌子:

                                      Table "public.parcel"
    Column    |          Type          |                            Modifiers
 ogc_fid      | integer                | not null default
 wkb_geometry | geometry(Polygon,4326) |
 county       | character varying      |
 parcel_area  | double precision       |

            Table "public.county"
 Column |          Type          | Modifiers
--------+------------------------+-----------
 name   | character(1)           |
 chull  | geometry(Polygon,4326) |
 area   | double precision       |

我想在parcel中找到县的所有唯一值,以及附加宗地的总区域,然后将其作为countyname表插入到area表中分别为{1}}。

我知道如何做到这一步的前半部分:

SELECT county, 
       SUM(parcel_area) AS area 
FROM inspire_parcel 
GROUP BY county;

但我不知道如何将这些值插入county。有人可以建议吗?

我认为它类似于:

UPDATE county SET name, area = (SELECT county, SUM(parcel_area) AS area 
FROM inspire_parcel GROUP BY county)

1 个答案:

答案 0 :(得分:2)

您使用INSERT INTO。所以,像这样:

INSERT INTO county
 SELECT county, SUM(parcel_area) AS area
  FROM inspire_parcel GROUP BY county;