Skip to content Skip to sidebar Skip to footer

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 age column from table2 to table1 (WHERE table1.id = table2.id

Solution 1:

First add the column with the appropriate datatype.

ALTERTABLE table1 ADDCOLUMN Age TINYINT UNSIGNED NOTNULLDEFAULT0;

Then update the table, so that the values are "transmitted".

UPDATE table1 t1
INNERJOIN tabel2 t2 ON t1.id = t2.id 
SET t1.Age = t2.Age

Solution 2:

First add Age column in table1

ALTERTABLE table1 ADDCOLUMN Age TINYINT UNSIGNED DEFAULT0;

then update that column using blow query

UPDATE table1 t1
INNERJOIN Tabel2 t2 ON t1.id = t2.id 
SET t1.age = t2.age;

Post a Comment for "How To Add A Column To A Table From Another Table In Mysql?"