简体   繁体   English

PostgreSQL:将table1复制到table2和table3,但使用返回的table2值插入table3

[英]postgresql: copy table1 to table2 and table3, but use returned table2 values to insert into table3

I have three tables: 我有三个表:

table1(id, name, foo) table1(id,name,foo)
table2(id, name) table2(编号,名称)
table3(id, id2, name, foo, bar) table3(id,id2,name,foo,bar)

And I'd like to copy data from table1 to tables 2 & 3, but table3.id2 has to correspond to the row in table2. 我想将数据从表1复制到表2和3,但是table3.id2必须与table2中的行相对应。 I think I need to do something with the RETURNING command, but I have been unsuccessful. 我想我需要用RETURNING命令做些事情,但是我一直没有成功。

WITH oldstuff AS (
    SELECT name, foo, 'some value' AS bar
    FROM table1
  ),
  newstuff AS (
    INSERT INTO table2 (name)
    SELECT name FROM oldstuff
    RETURNING id AS id2 /* but i also need oldstuff.name, oldstuff.foo,and oldstuff.bar */
  )
INSERT INTO table3 (id2, name, foo, bar)
SELECT * FROM newstuff; /* I cant just do a join here because the fields arent unique */

Conceptually, you would do: 从概念上讲,您将执行以下操作:

WITH oldstuff AS (
      SELECT name, foo, 'some value' AS bar
      FROM table1
     ),
     newstuff AS (
      INSERT INTO table2 (name)
          SELECT name
          FROM oldstuff
          RETURNING *
    )
INSERT INTO table3 (id2, name, foo, bar)
    SELECT ns.id, ns.name, os.foo, os.bar
    FROM newstuff ns join
         oldstuff os
         on ns.name = os.name;

From what you say, this doesn't do exactly what you want because there is not a unique name in newstuff . 用您的话说,这并不能完全满足您的要求,因为newstuff没有唯一的名称。 Either generate a unique id for the original table or only insert unique data into it: 为原始表生成唯一的ID或仅向其中插入唯一的数据:

      INSERT INTO table2 (name)
          SELECT DISTINCT name
          FROM oldstuff
          RETURNING *;

Hmmm . 嗯。 . . There might be a kludgy way: 可能有一种怪诞的方式:

WITH oldstuff AS (
      SELECT name, foo, 'some value' AS bar
      FROM table1
     ),
     newstuff AS (
      INSERT INTO table2 (name)
          SELECT name
          FROM oldstuff
          RETURNING *
    )
INSERT INTO table3 (id2, name, foo, bar)
    SELECT ns.id, ns.name, os.foo, os.bar
    FROM (SELECT ns.*, ROW_NUMBER() OVER (PARTITION BY name ORDER BY name) as seqnum
          FROM newstuff ns
         ) ns JOIN
         (SELECT os.*, ROW_NUMBER() OVER (PARTITION BY name ORDER BY name) as seqnum
          FROM oldstuff os
         ) os
         on ns.name = os.name and ns.seqnum = os.seqnum;

This will work with duplicate names, and you get exactly one match in the final table. 这将使用重复的名称,并且在决赛桌中您将获得一个完全匹配的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 插入table1从table2,table3中选择,其中 - insert table1 select from table2,table3 where 从一个表1中选择多个值,在表2中查找并在表3中插入 - Select multiple values from one table1, find in table2 and insert in table3 SQL 获取 table1 的名称以及 table2 和 table3 的计数 - SQL get table1 names with a count of table2 and table3 从table1,table2,table3删除在哪里? - delete from table1,table2,table3 where? sql server将数据从table1插入table2,其中table1 = table3数据 - sql server insert data from table1 to table2 where table1=table3 data 除了Table3中已经存在的条目之外,如何将Table1和Table2的交集插入到Table3中? - How can I insert into Table3 the intersection of Table1 and Table2, except the entries that already exist in Table3? 从table1插入一个table2获取id然后插入table3 SQL服务器 - Insert into one table2 from table1 get the id and then insert into table3 SQL server SQL比较,其中在表2中找不到表1的值,并将结果显示为表3 - Sql Comparison where values of Table1 not found in Table2 and showing result as Table3 根据 table3 中的条件从 table2 插入 table1 - Insert into table1 from table2 based on conditions from table3 遍历table1,从table2中提取信息并插入到Teradata中的table3中 - Loop through table1, extract information from table2 and insert into table3 in Teradata
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM