使用另一个表的列中的数据更新列

时间:2014-04-15 12:52:27

标签: mysql sql-update inner-join

假设我在MySQL数据库中有两个表。我想将源表中的列中的数据更新为目标表中的列。

  

源表名为' Computers_F1' “源列”称为“家庭'。

”      

目标表被称为'建议'目标列称为' Fase1'

我想把“家庭”中的所有行都放在'进入' Fase1'。但是没有它添加新行。我希望在现有行中更新所有信息。

希望你们能帮助我!

2 个答案:

答案 0 :(得分:0)

如果你有一个匹配

的列ID,你可以这样做
UPDATE Advice AS a
SET Fase1 = (SELECT Family FROM Computers_F1 AS c WHERE a.id = c.id)

然后重命名列Fase1

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

(用正确的列格式替换VARCHAR(255))

答案 1 :(得分:0)

我知道两种方式:

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;

希望这会有所帮助......

更新

使用外键修正示例:

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;