MySQL Forums
Forum List  »  Newbie

Re: Add new colums to one existing table with copying from another existing table
Posted by: Barry Galbraith
Date: October 16, 2016 03:07PM

So you want table_2 to have temc, name_x, name_y, name_a, name_b ?
Your ALTER ... is the way to add the columns.

ALTER TABLE table_2   
  ADD COLUMN name_a VARCHAR(20) NULL AFTER name_y,
  ADD COLUMN name_b VARCHAR(20) NULL AFTER name_a
  ;

Then populate them with
UPDATE table_2 t2
JOIN table_1 t1
ON t1.temc = t2.temc
SET t2.name_a = t1.name_a,
t2.name_b = t1.name_b
;

If that isn't what you want, then will need to be more explicit with your requirement.

Good luck,
Barry.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.