2015-02-11 53 views
0

我有兩個表。如何從其他表中插入值到現有表

Table1 , Table2. 
Table1 has name, age, salary. 
Table2 has name,height, weight,Relevance,Weight_po etc. 

。這兩個表的名稱都是主鍵。 現在我想在表1中插入兩個新的列,即高度,重量。 高度和重量的值必須從Table2中獲取,其中table1.name與table2.name匹配。

幫助我如何在postgres中實現這一點。

+0

什麼部分造成你的困難? – 2015-02-11 14:04:35

回答

0

可以使用select into語句並插入你希望所有的數據到newtable的與結構你描述:

編輯:基於評論

Create table NewTable as 
    SELECT Table1.name,age,salary,height,weight  
    INNER JOIN Table2 
    ON Table1.name=Table2.name 
+0

我得到錯誤錯誤:表「表1」的FROM-clause條目的無效引用 – Ramkumar 2015-02-11 14:22:02

+0

對於錯字我很抱歉。我的表2有更多的柱子像名稱,高度,重量,直徑,weight_po,相關性。但我只需要身高和重量 – Ramkumar 2015-02-11 14:24:17

+0

也許我的編輯幫助 – apomene 2015-02-11 14:25:58

0

有您創建的身高和體重列在表1中?

這將有助於填充值:

UPDATE Table1 t1 
SET height= t2.height,weight=t2.weight 
FROM Table2 t2 
WHERE t1.name= t2.name 

告訴我是怎麼回事

相關問題