'How to Join two tables and update a column?
I have two tables as below
TableA TableB
ID1 ID2 ID1 ID3
1 X 1 X
2 NULL 2 A
3 (NULL) 3 B
4 (NULL) 4 C
5 (NULL) 5 D
Expected Result:
TableA
ID1 ID2
1 X
2 A
3 B
4 C
5 D
My query:
UPDATE TableA a LEFT JOIN TableB b ON a.ID1 = b.ID1
SET a.ID2 = b.ID3
WHERE a.ID2 is NULL OR a.ID2 != b.ID3
But the above query is going in a loop. Thanks for your help in advance.
Solution 1:[1]
If your tables look exactly like the ones you have in your question then you have no need to join the tables, your resulting table is the table on the right.
select *
from table b;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ereniss |