簡體   English   中英

將列數據行(全部)插入到另一列表中,其中每一行都與公共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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM