如何创建这个postgres查询INSERT?

时间:2017-05-17 09:51:40

标签: postgresql pg

我需要返回第一个表的id并将其填入其他2个表中。

我该怎么做?

                                     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($1, $2, $3, $4)',
    [sog.nome, sog.cognome, sog.ragioneSociale, sog.partitaIva],'INSERT INTO "Tipologia"(privato, azienda) VALUES ($5, $6)',
    [sog.privato, sog.azienda],'INSERT INTO "Tipologia2"(cliente, fornitore) VALUES($7, $8)',[sog.cliente, sog.fornitore]

2 个答案:

答案 0 :(得分:0)

使用CTE:

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

name5,6,7,8用于我不知道的表“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)

答案 1 :(得分:0)

  

以这种方式解决:

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

谢谢,Vao!