我有两个表table1和table2。
我正在做一些更改,我意识到不需要 table2,但该表已经有很多数据,我需要从 传递 ID_B 的值表2到表1。
结构如下:
表1
ID_table1 | ID_table2 | ID_B
1 | 1 |
2 | 3 |
3 | 1 |
4 | 2 |
表2
ID_table2 | ID_B
1 | 14
2 | 26
3 | 26
所以我想要的是MySQL查询将ID_B值从table2传递到table1,当ID_table2 table1 上的 > 等于 table2 上的 ID_table2。
例如,table1 上 ID_table1 为 1 的行的 ID_B = 14。
你能帮我解决这个问题吗? 提前致谢,
米格尔。
最佳答案
使用 JOIN 您可以执行以下操作。
update table1 t1
inner join
table2 t2 on t2.ID_table2 = t1.ID_table2
set t1.ID_B = t2.ID_B
<强> DEMO
关于mysql - 使用另一个表中的值更新表新列 (MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23246318/