0

I have two tables which is 'table1' and 'table2' , table1 looks like this

elements
facebook
whatsapp
youtube
facebook
youtube

and other table table2 looks like this

id e
1 youtube
2 facebook
3 whatsapp

i want to make column "e_id" in table1 based on element's "id" in table2

so the result may look like this

elements e_id
facebook 2
whatsapp 3
youtube 1
facebook 2
youtube 1

the e_id should be dynamic ( like if element is found in the table2, it will assign its id in e_id column )

is there a optimized way to do this in postgreSQL?

3
  • 1
    Hi, where is your code ? What have you tried ? Commented Apr 27, 2022 at 8:17
  • i am currently using dbt tool, and i made table2 as a dict and compared with them with for loop for the column e_id Commented Apr 27, 2022 at 8:21
  • Does this answer your question? how to create column based on other column in sql? Commented Apr 27, 2022 at 8:36

1 Answer 1

1

Not sure about optimized way but you need to alter your table1 first then you do an update on it. cheers!

ALTER TABLE table1
ADD COLUMN e_id int;

UPDATE table1
SET table1.e_id = table2.id
FROM table2
WHERE table1.elements = table2.e
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.