MySQL Forums
Forum List  »  Newbie

Re: Insert keys from 1 table into 2 others.
Posted by: Rick James
Date: March 16, 2012 07:00PM

Did you really want all rows to say 'default'?

Assuming Newtable is really a "normalization" of Value, then start over on Newtable, this time including
UNIQUE(Value)
Then...
INSERT IGNORE INTO Newtable
    SELECT DISTINCT Value  FROM TableA;
UPDATE  TableA JOIN Newtable ON TableA.Value = Newtable.Value
    SET TableA.new_id = Newtable.id;

INSERT IGNORE INTO Newtable
    SELECT DISTINCT Value  FROM TableB;
UPDATE  TableB JOIN Newtable ON TableB.Value = Newtable.Value
    SET TableB.new_id = Newtable.id;

"IGNORE" Is not necessary on the first one, but it makes the SQL consistent.

Options: ReplyQuote


Subject
Written By
Posted
Re: Insert keys from 1 table into 2 others.
March 16, 2012 07:00PM


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.