ava*_*123 8 postgresql join insert
我正在从连接两个表的查询中插入,然后我想要插入行中的新 ID,以及插入中不涉及的原始行中的字段。是否可以?我收到“列不存在”错误。
INSERT INTO new_table (x,y)
select A.x,B.y
from A
join B on A.w = B.z
RETURNING id,B.z;
Run Code Online (Sandbox Code Playgroud)
对new_table有唯一约束(x,y)。
在new_id和B.z需要插入到第二个表。
ype*_*eᵀᴹ 10
也许有更好的选择,但我只能想到加入 2 个表。
这假设new_table具有唯一约束(x,y)并且这些列不可为空:
with ins (id, x, y) as
( insert into new_table (x, y)
select A.x, B.y
from A join B on A.w = B.z
returning id, x, y
)
-- insert into another_table (id, z)
select
ins.id, B.z -- whatever columns from the 3 tables
from ins
join A on A.x = ins.x
join B on B.y = ins.y and A.w = B.z ;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5630 次 |
| 最近记录: |