根据之前的插入插入多行

问题描述

我想在表 A 中插入多行,并使用这些创建的行的 id 在桥表 B 中插入多行,每个 id 有两个条目。

基本上,表 B 中 A 中每个创建的行应该有两行。

解决方法

您可以使用带有 returning 子句的 CTE:

with i as (
      insert into a ( . . . )
          . . . 
          returning *
     )
insert into bridge (a_id,other_id)
    select i.id,v.other_id
    from i cross join
         (values ('x'),('y')) v(other_id);