Rithik_Star
Rithik_Star

Reputation: 661

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

I am having two tables .

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

. Both the tables has name as primary key. Now I want to insert a two more new column in table 1 ie height, weight. The values for height and weight has to fetch from Table2 where table1.name matches with table2.name.

Help me how to achieve this in postgres.

Upvotes: 0

Views: 129

Answers (2)

Simon Mbatia
Simon Mbatia

Reputation: 301

Have you created height and weight columns in Table 1?

This will help to populate the values:

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

Tell me how it goes

Upvotes: 0

apomene
apomene

Reputation: 14389

You can use select into statement and insert all data you want onto a NewTable with the structure you describe:

EDIT: Based on comment

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

Upvotes: 0

Related Questions