![](/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.