mihsathe
mihsathe

Reputation: 9154

Join two tables with one table

I need to do the following (I am not really sure if it is possible to do using SQL)

Table A

col_a_1, col_a_2, col_a_3

Table B

col_a_1, col_b_1, col_b_2....

Table C

col_a_1, col_c_1, col_c_2....

were col_a_1 is unique in table A. I need to join tables A, B, C such that every entry in table A will appear multiple times but every entry in table B and C appears only once and against the same col_a_1 of table A.

I know how to do it using the code. Is it also possible to do with SQL alone?

Thanks in advance.

Upvotes: 1

Views: 133

Answers (1)

xQbert
xQbert

Reputation: 35323

You either want to use an outer join of some sort. either left or full outer.

SELECT A.*, B.*, C.* 
FROM Table_A A
LEFT JOIN Table_B B on A.col_A_1 = B.Col_A_1
LEFT JOIN table_C C on A.Col_A_1 = C.Col_A_1


SELECT A.*, B.*, C.* 
FROM Table_A A
FULL OUTER JOIN Table_B B on A.col_A_1 = B.Col_A_1
FULL OUTER JOIN table_C C on A.Col_A_1 = C.Col_A_1

or possibly a union result... just depends on what you're after.

SELECT A.*, B.* 
FROM Table_A A
INNER JOIN Table_B B on A.col_A_1 = B.Col_A_1
UNION
SELECT A.*, C.* 
FROM Table_A A
INNER JOIN Table_C C on A.col_A_1 = C.Col_A_1

Upvotes: 2

Related Questions