如何创建此 postgres 查询 INSERT?

How to create this postgres query INSERT?

我需要return第一个table的id填入另外2个table。

我该怎么做?

                                     Table "public.Soggetto"
   Column   |           Type           |                        Modifiers
------------+--------------------------+---------------------------------------------------------
 codFisc    | character varying(20)    |
 partIVA    | character varying(20)    |
 regSociale | character varying(100)   |
 nome       | character varying(20)    |
 cognome    | character varying(20)    |
 gruppo_id  | integer                  |
 dataIns    | timestamp with time zone |
 dataElim   | timestamp with time zone |
 id         | integer                  | not null default nextval('"Soggetto_id_seq"'::regclass)

Indexes:
    "Soggetto_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "TabGruppo" FOREIGN KEY (gruppo_id) REFERENCES "Gruppo"(id)
Referenced by:
    TABLE ""JunctionONE"" CONSTRAINT "ToSoggetto" FOREIGN KEY (soggetto_id) REFERENCES "Soggetto"(id)
    TABLE ""Tipologia"" CONSTRAINT "toSoggetto" FOREIGN KEY (soggetto_id) REFERENCES "Soggetto"(id) ON UPDATE CASCADE ON DELETE CASCADE
    TABLE ""Tipologia2"" CONSTRAINT "toSoggetto" FOREIGN KEY (soggetto_id) REFERENCES "Soggetto"(id) ON UPDATE CASCADE ON DELETE CASCADE





var query = client.query('INSERT INTO "Soggetto" (nome, cognome, "regSociale", "partIVA") VALUES(, , , )',
    [sog.nome, sog.cognome, sog.ragioneSociale, sog.partitaIva],'INSERT INTO "Tipologia"(privato, azienda) VALUES (, )',
    [sog.privato, sog.azienda],'INSERT INTO "Tipologia2"(cliente, fornitore) VALUES(, )',[sog.cliente, sog.fornitore]

使用 CTE:

var query = client.query(
'with i as (
   INSERT INTO "Soggetto" (nome, cognome, "regSociale", "partIVA") 
   VALUES(, , , ) RETURNING *
 )
 , e as (
   INSERT INTO "Tipologia"(soggetto_id,privato, azienda) select SELECT id, , 
   FROM i
 )
 INSERT INTO "Tipologia2"(soggetto_id,cliente, fornitore) select SELECT id,, 
   FROM i
',[sog.nome, sog.cognome, sog.ragioneSociale, sog.partitaIva, sog.privato, sog.azienda, sog.cliente, sog.fornitore]);

name5,6,7,8 用于我不知道的 table "Soggetto" 的列名称 - 请更改为原始名称

已知名称的工作示例:

t=# create table s141 (i int);
CREATE TABLE
t=# create table s142 (i int);
CREATE TABLE
t=# create table s143 (i int);
CREATE TABLE
t=# with i as (insert into s141 select 1 returning *)
, e as (insert into s142 select i from i)
t-# insert  into s143 select i from i;
INSERT 0 1
t=# select * from s141;
 i
---
 1
(1 row)

t=# select * from s142;
 i
---
 1
(1 row)

t=# select * from s143;
 i
---
 1
(1 row)

Solved in this way:

  var query = client.query('WITH i AS (INSERT INTO "Soggetto" (nome, cognome, "regSociale", "partIVA") VALUES (, , , ) RETURNING id), e AS (INSERT INTO "Tipologia" (privato, azienda, soggetto_id) SELECT , , id FROM i ) INSERT INTO "Tipologia2" (cliente, fornitore, soggetto_id) SELECT , , id FROM i ', [sog.nome, sog.cognome, sog.ragioneSociale, sog.partitaIva, sog.cliente, sog.fornitore, sog.privato, sog.azienda],

Thanks, Vao!