2016-11-07 61 views
1

我需要在其中引用另一個表中的id字段的字段。 id字段它是去旁邊的現場「測試」(列 - codedescription,表typecategory)和ID字段來字旁「評估」(列categorydescription,表typecategory)將單個字段插入表中,引用另一個表

INSERT INTO codetype 
(typecategoryid) 
Where codedescription='test' 
SELECT id FROM typecategory WHERE categorydescription='Assessment Types' 

有很多插入整列的例子,但沒有人寫過如何從另一個表中插入單個字段。

表 - CODETYPE ID BIGSERIAL主鍵 codedescription VARCHAR
typecategoryid BIGINT外鍵typecatogory上的ID列

表 - typecategory ID大系列主鍵 categorydescription VARCHAR

+0

是否使用的是RDBMS?當你運行這個腳本時會發生什麼?你是否遇到了錯誤,或者只是不是你期望的結果? – CGritton

+0

要澄清一下,您正在嘗試在填充它的同時創建「typecategoryid」字段? – CGritton

+0

我正在使用postgres。有一個包含大量信息的列typecategoryid。我需要從一個不同的表中填充最後三個字段(類型) –

回答

0

如果列已經存在並且表中其餘列中已經有記錄,那麼您需要UPDATE聲明,而不是INSERT

貌似這篇文章可以幫助你:Update a column of a table with a column of another table in PostgreSQL

也許

UPDATE codetype c 
SET c.typecategoryid = t.id 
FROM typecategory t 
WHERE c.codedescription = 'test' and t.categorydescription='Assessment Types' 
+0

UPDATE CODETYPE (typecategoryid) WHERE codedescription = '測試' SET typecategor.id = codetype.typecategoryid WHERE typecategoryid = 4 ??? –

+0

對不起。我不知道如何格式化評論 –

+0

我感謝您的幫助。非常感謝你。 –

相關問題