MySQL根据另一个表值更新表

2021-11-20 00:00:00 join sql-update mysql

我有两张桌子,

这是我的第一张桌子,

ID      SUBST_ID        CREATED_ID
1       031938          TEST123
2       930111          COOL123
3       000391          THIS109
4       039301          BRO1011
5       123456          COOL938
...     ...             ...

这是我的第二张桌子,

ID      SERIAL_ID       BRANCH_ID
1       039301          NULL
2       000391          NULL
3       123456          NULL
...     ...             ...

我需要了解如何使用第一个表中的数据更新第二个表中的所有行.

I need to some how update all rows within my second table using data from my first table.

它需要在一个更新查询中完成这一切.

It would need to do this all in one update query.

SUBST_ID 和 SERIAL_ID 都匹配,需要从第一个表中获取 created_id 插入到第二个表中.

Both SUBST_ID and SERIAL_ID match, it needs to grab the created_id from the first table and insert it into the second table.

所以第二个表会变成下面这样,

So the second table would become the following,

ID      SERIAL_ID       BRANCH_ID
1       039301          BRO1011
2       000391          THIS109
3       123456          COOL938
...     ...             ...

感谢您的帮助和指导.

推荐答案

UPDATE TABLE2
       JOIN TABLE1
       ON TABLE2.SERIAL_ID = TABLE1.SUBST_ID
SET    TABLE2.BRANCH_ID = TABLE1.CREATED_ID;

相关文章