Question

Let's say I have two tables in a MySQL database. I would like to update the data from a column in the source table to a column in the target table.

Source table is called 'Computers_F1' Source column is called 'Family'.

Target table is called 'Advice' Target column is called 'Fase1'

I want to put all the rows from 'Family' into 'Fase1'. But this without it adding new rows. I want all the information to be updated in the existing rows.

Hope you guys can help me!

Was it helpful?

Solution

You can do something like this if you have a column id to match both

UPDATE Advice AS a
SET Fase1 = (SELECT Family FROM Computers_F1 AS c WHERE a.id = c.id)

And then rename the column Fase1

ALTER TABLE Active CHANGE `Fase1` `Family` VARCHAR(255)

(replacing VARCHAR(255) with the right format of your column)

OTHER TIPS

I know 2 ways:

UPDATE Advice, Computers_F1
SET Advice.Fase1 = Computers_F1.Family 
WHERE Advice.Id = Computers_F1.Id;

OR

UPDATE Advice
INNER JOIN Computers_F1
ON Advice.Id = Computers_F1.Id
SET Advice.Fase1 = Computers_F1.Family;

Hope this helps...

UPDATE

Correct examples, with foreign keys:

UPDATE Advice, Computers_F1
SET Advice.Fase1 = Computers_F1.Family 
WHERE Advice.Computers_F1_Id = Computers_F1.Id;

OR

UPDATE Advice, Computers_F1
SET Advice.Fase1 = Computers_F1.Family 
WHERE Advice.Id = Computers_F1.Advice_Id;

OR

UPDATE Advice
INNER JOIN Computers_F1
ON Advice.Computers_F1_Id = Computers_F1.Id
SET Advice.Fase1 = Computers_F1.Family;

OR

UPDATE Advice
INNER JOIN Computers_F1
ON Advice.Id = Computers_F1.Advice_Id
SET Advice.Fase1 = Computers_F1.Family;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top