Skip to content Skip to sidebar Skip to footer

Join Two Tables Based On Two Columns

Hi my SQL is a bit rusty and need a bit help with getting a statement correct. I have the following setup table A and B: A B ===== ====== A A B B C B

Solution 1:

this is how you could join A's column on Both columns of B.

SELECT*FROM A
INNERJOIN B B1 ON A.Column1 = B1.Column1 
INNERJOIN B B2 ON A.Column1 = B2.Column2 

Or you can use UNION, to combine two query results

SELECT*FROM A
INNERJOIN B  ON A.Column1 = B.Column1 
UNIONSELECT*FROM A
INNERJOIN B  ON A.Column1 = B.Column2 

you can also consider LEFT joins, depending on how you want to handle NULL values in B.

Post a Comment for "Join Two Tables Based On Two Columns"