ElegantFellow ElegantFellow - 4 years ago 74
SQL Question

join on multiple columns

I have two tables (Table A and Table B) which I want to join on multiple columns in both tables.

Table A
Col1 Col2
A11 A21
A22 A22
A33 A23

Table B
Col1 Col2 Val
B11 B21 1
B12 B22 2
B13 B23 3

I want both Columns in Table A to join on either of Col1 and Col2 in Table B to get Val.

Answer Source

Agree no matches in your example.
If you mean both columns on either then need a query like this or need to re-examine the data design.

    Select TableA.Col1, TableA.Col2, TableB.Val
    FROM TableA
          ON TableA.Col1 = TableB.Col1 OR TableA.Col2 = TableB.Col2 
          OR TableA.Col2 = TableB.Col1 OR TableA.Col1 = TableB.Col2
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download