Lets say I have two tables
NAME_TABLE
| ID1 | ID2 | NAME |
|---|---|---|
| A | 1 | JACK |
| B | 2 | CRAIG |
| C | 3 | RYAN |
| D | 4 | LARRY |
JOB_TABLE
| ID1 | ID2 | JOB |
|---|---|---|
| A | 2 | ENGINEER |
| B | 1 | TEACHER |
| E | 3 | ANALYST |
| F | 4 | ARCHITECT |
I want to make a query that first checks compares the ID1 of each table, and join them where they match. If there is no match in ID1, then we compare the ID2s.
So the final table would look like this. Notice that if the ID1s match, we don't care about doing anything with the ID2. We only care about ID2 if there is no ID1 match:
FINAL_TABLE
| NAME | JOB |
|---|---|
| JACK | ENGNEER |
| CRAIG | TEACHER |
| RYAN | ANALYST |
| LARRY | ARCHITECT |