How to add a column to a table from another table in Mysql?

I have two tables

  • table1
  • table2

Tabel1 contains 2 columns

  • ID
  • Name

Tabel2 contains 2 columns

  • ID
  • Age

A want to add an age column from table2 to table1 (WHERE table1.id = table2.id)

Then table1 should contain 3 columns

  • ID
  • Name
  • Age
+5
source share
2 answers

First add the Age column in table 1

ALTER TABLE table1 ADD COLUMN Age TINYINT UNSIGNED DEFAULT 0; 

then refresh this column using delete request

 UPDATE table1 t1 INNER JOIN Tabel2 t2 ON t1.id = t2.id SET t1.age = t2.age; 
+6
source

First add a column with the appropriate data type.

 ALTER TABLE table1 ADD COLUMN Age TINYINT UNSIGNED NOT NULL DEFAULT 0; 

Then refresh the table so that the values ​​are "passed".

 UPDATE table1 t1 INNER JOIN tabel2 t2 ON t1.id = t2.id SET t1.Age = t2.Age 
+30
source

Source: https://habr.com/ru/post/1208642/


All Articles