![](/img/trans.png)
[英]Select rows from a filtered portion of Table A where a column matches a relationship with a column from the row in Table B that matches by ID
[英]Insert column data rows (all) in another column table where each row matches the common ID
Table1 Table2
column1ID | column1 column2ID | column2
1 | A 1 | copyofA
2 | B 2 | copyofB
根据要求,我想在table2中插入copyofA copyofB行等(约54k行),但column2ID必须与已经填充的column1ID相匹配。
按照标题,我想在另一列表中插入列数据行(全部),其中每一行与公共ID列值匹配
我用了通常的命令
INSERT INTO table2 (column2)
SELECT column1
FROM table1
但不幸的是,似乎ID值的列需要某种比较才能成功
输出错误
[Err] ERROR: insert or update on table "table2" violates foreign key constraint "table2_column2ID_fkey"
DETAIL: Key (column2ID)=(400992) is not present in table "table2".
最后要添加的是,table2显然具有附加到table1的主键ID的外键
我认为您想要update
:
update table2 t2
set column2 = t1.column1
from table1 t1
where t2.id = t1.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.