基于来自单独数据库的ID更新表

时间:2015-09-02 19:51:19

标签: mysql database join

我需要将DATABASE_1中的ID_1从DATABASE_1切换到ID_2,DATABASE_2,其中DATABASE_1.ID_1 = DATABASE_2.ID_3

UPDATE `table_1` 
SET `DATABASE_1`.`table_1`.`ID_1`= `DATABASE_2`.`table_2`.`ID_2`
WHERE `DATABASE_1`.`table_1`.`ID_1` = `DATABASE_2`.`table_2`.`DealerID`

以上是我想到的陈述类型。这是结构

DATABASE_1
table_1
    ID_1
    1
    2
    3
    4
    5

DATABASE_2
table_2
    ID_2            ID_3
    10              1               
    20              2
    30              3
    40              4
    50              5

我想我需要以某种方式加入这些......或者在table_1上添加列并更改它们。我很难加入,到目前为止我已经

SELECT ID_2 , ID_3 FROM DATABASE_2.table_2 t2 JOIN DATABASE_1.ID_1 t2 ON t1.ID_1 = t2.ID_3

但是没有更新它......

1 个答案:

答案 0 :(得分:0)

您需要JOIN

UPDATE DATABASE_1.table_1 AS t1
JOIN DATABASE_2.table_2 AS t2 ON t1.ID_1 = t2.DealerID
SET t1.ID_1 = t2.ID_2