Insert with Left Join and multiple columns

38 views Asked by At

I have the following problem, i have 2 tables:

Table A (with the columns "ColumnA" and "ColumnB")

ColumnA ColumnB
111 555
111 333
111 444
222 555
222 666

Table B (with the columns "ColumnC" and "ColumnD")

ColumnC ColumnD
111 222
111 333
111 444
222 444

i want to insert the values of table B into table A, as long as a combination of both values is not yet in table A (by joining ColumnA/ColumnC and ColumnB/ColumnD). I know how to work with a left join (and the where column... is null) to add values for one column that is not yet available but i don't know how to do it with two columns (as a combination of two columns).

In other words in my case only these two would be added because the combination of those two is not yet available in table A (i hope it's clear what i mean):

111 222
222 444

Tried it with a left join and where column is null clause but couldn't make it work for two columns.

This is what i have so far:

select *  from TableA 
left  join TableB on TableA.ColumnB = TableB.ColumnD
and TableB.ColumnA = TableA.ColumnC
where ...
0

There are 0 answers