如何将其他 table 的值插入现有 table

How to insert a value from other table into the existing table

我有两个 tables .

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

。 table 都将名称作为主键。 现在我想在 table 1 中再插入两个新列,即身高、体重。 身高和体重的值必须从表 2 中获取,其中 table1.name 与 table2.name 匹配。

帮助我如何在 postgres 中实现这一点。

您可以使用 select into 语句并将您想要的所有数据插入到具有您描述的结构的 NewTable 中:

编辑: 基于评论

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

您是否在 Table 1 中创建了身高和体重列?

这将有助于填充值:

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

告诉我进展如何