I have four tables:
Table a:
ID | B_ID ---------- 1 | 5 2 | 6 3 | 7 4 | 8
Table B:
B_ID ----- 5 6 7 8
Table C:
C_ID | C_Name -------------- 5 | Alpha 6 | Beta
Table D:
D_ID | D_Name -------------- 7 | Delta 8 | Gamma
Note that the values ββin table B can be obtained from table C or table D.
Now I need a query that shows the ID from table A and a second column named Name , which consists of the corresponding names based on column B_ID table B.
The expected result should look like this:
ID | Name ---------- 1 | Alpha 2 | Beta 3 | Delta 4 | Gamma
I tried this query:
SELECT * FROM B LEFT OUTER JOIN C ON B_ID = C_ID LEFT OUTER JOIN D ON B_ID = D_ID
This gives:
B_ID | C_ID | C_Name | D_ID | D_Name ------------------------------------- 5 | 5 | Alpha | Null | Null 6 | 6 | Beta | Null | Null 7 | Null | Null | Null | Delta 8 | Null | Null | Null | Gamma
However, I still have two problems:
- I need to combine the names into one column (see the expected result above)
- It should be a
SELECT subquery based on Table A to show the column ID table A.
source share