'Select all from Table A, replace values from Table B
So I have this kind of tables:
Lots of values in Tabel A
TABLE_A
ID | TEXT
----------
1 | 'aaa1'
2 | 'aaa2'
3 | 'aaa3'
4 | 'aaa4'
5 | 'aaa5'
6 | 'aaa6'
Some values in Table B:
TABLE_B
ID | TEXT
----------
4 | 'bbb4'
5 | 'bbb5'
And I want result to give
RESULT
ID | TEXT
----------
1 | 'aaa1'
2 | 'aaa2'
3 | 'aaa3'
4 | 'bbb4'
5 | 'bbb5'
6 | 'aaa6'
Solution 1:[1]
Use a left outer join to join the two tables, and a coalesce() function to select values from table B when they exist.
select a.id
,coalesce(b.text, a.text) as text
from table_a a
left outer join table_b b
on b.id = a.id
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 | APC |
