简体   繁体   中英

copy data from 2 separate tables in postgresql

Just wondering is it possible to extract data from 2 different tables at one time in postgresql

I have the following:

Blocks Table - has been created as follows in order to fit a schema, so the JSON information has all been stored in an information column containing 36 polygons each

UUID (UUID) Name (TEXT) Type (TEXT) Information (TEXT)
815b2ce7-ce99-4d6c-b41a-bec512173f53 C2 Block 'stored JSON info'
7a9a03fc-8be6-47ca-b743-43715ebb5610 D2 Block 'stored JSON info'
9136dcda-2a55-4084-87c1-68ccde23aed8 E3 Block 'stored JSON info'

For a later query, I need to know the geometries of each of the polygons, so I created another table using a code which parsed them out:

CREATE TABLE blockc2_ AS SELECT geom FROM (SELECT elem->>'type' AS type, elem->'properties' AS prop, elem->'geometry' AS geom FROM (SELECT json_array_elements(data) elem FROM block) f1)f2;

A final table is created to show just the geometries (which will a associated with the already created UID's like below

new_table

UUID (UUID) Geometry (Geometry)
815b2ce7-ce99-4d6c-b41a-bec512173f53 01030000000100000005000000972E05A56D6851C084D91C434C6C32401C05D4886B6851C086D974FA4D6C324078F4DA916D6851C036BF7504766C3240F31D0CAE6F6851C035BF1D4D746C3240972E05A56D6851C084D91C434C6C3240
7a9a03fc-8be6-47ca-b743-43715ebb5610 01030000000100000005000000BB05694F726851C0CB2A87A8486C32403EDC3733706851C0CD2ADF5F4A6C32409ACB3E3C726851C07E10E069726C324017F56F58746851C07C1088B2706C3240BB05694F726851C0CB2A87A8486C3240
9136dcda-2a55-4084-87c1-68ccde23aed8 1030000000100000005000000972E05A56D6851C084D91C434C6C32401C05D4886B6851C086D974FA4D6C324078F4DA916D6851C036BF7504766C3240F31D0CAE6F6851C035BF1D4D746C3240972E05A56D6851C084D91C434C6C3240

Ideally, I need a code like below (if its possible) because if I insert them separately they don't associate with each other. Instead of 3 rows of info, it will be 6 (3 UUIDS and 3 Geometries)

INSERT INTO new_table (uuid, geometry) SELECT UUID FROM blocks WHERE Name='C2' AND SELECT geometry FROM second_table WHERE Name='C2'

Is something like this possible?

create table C (select * from table B union all select * from table A)

This sounds like a join :

INSERT INTO new_table (uuid, geometry)
    SELECT b.UUID, g.geometry
    FROM blocks b JOIN 
         geometry g
         USING (name)
    WHERE Name = 'C2'; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM