Skip to content
Advertisement

How to LEFT JOIN with multiple cases

Assuming I have these two tables like:

Table: tab1

valA valB valC valD
1 11 111 A
2 11 333 A
3 44 444 B
3 66 666 D

Table: tab2

val1 val2 val3 val4
1 11 111 A
1 22 222 A
2 44 333 B
3 55 555 A
3 66 666 D

I have 3 cases that I have unite with UNION. This is the code:

The result would be:

table: tog

valA valB valC valD val1 val2 val3 val4
1 11 111 A 1 11 111 A
1 11 111 A 1 22 222 A
2 33 333 a 2 44 444 B
3 66 666 D 3 66 666 D

So, I need the table of tab1 but without the matches, like:

valA valB valC valD val1 val2 val3 val4
3 44 444 B

I tried something like this, but I know this is totaly wrong:

I am pretty new on SQL so I would appreciate any tipps and suggestions.

Advertisement

Answer

You want to select data from tab1 where not exists a match in tab2.

Or, if you need the empty tab2 columns:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement