[英]Update table new column with values from another table (MySQL)
我有兩個表table1和table2 。
我進行了一些更改,我意識到不需要table2 ,但是此表已經有很多數據,我需要將ID_B的值從table2傳遞到table1 。
結構如下:
ID_table1 | ID_table2 | ID_B
1 | 1 |
2 | 3 |
3 | 1 |
4 | 2 |
ID_table2 | ID_B
1 | 14
2 | 26
3 | 26
因此,我要的是MySQL查詢從表2通過ID_B值table1時就表1的ID_table2等於ID_table2對表2。
例如,表1上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
您可以這樣嘗試:
UPDATE
table1 AS target,
(SELECT ID_table2, ID_B FROM table2) AS source
SET
target.ID_B = source.ID_B
WHERE
target.ID_TABLE2 = source.ID_table2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.