I need to get NULL values in Columns if Intermediate table doesn't apply
I am joining tables where there are two separate relationship schemes look like so:
TableA1 -> TableA2 -> TableB -> TableC : <<< RELATIONSHIP A
enter code here
And the output needs to look like so:
A1colu开发者_如何学CmnId A2columnId BcolumnId CcolumnId
1 1 1 1
2 null 2 2
3 null 3 3
4 2 4 4
5 null 5 5
If there exists these two relationship A & B , I need to see A2colId to be null for below relationship:
And the 2nd relationship scheme looks like so: TableA1->TableB-TableC <<<< RELATIONSHIP B (This scheme has no clue about TableA2)
How would you do the join to get the result set with nulls in A2ColumnId as depicted above?
LEFT JOIN
instead of a regular join will force the table to join even if no data exists in the right table, filling in null
in its place. So when you join A1 and A2 you would do so:
SELECT *
FROM A1
LEFT JOIN A2
ON A1.idlink = A2.idlink
JOIN B
ON A1.id = B.id
JOIN C
ON A1.id = C.id;
SELECT *
FROM TableA A1
LEFT JOIN TableA A2 ON A1.ColumnId1 = A2.ColumnId2
INNER JOIN TableB B ON B.ColumnId IN (A1.ColumnId1, A2.ColumnId2)
INNER JOIN TableC C ON B.ColumnId = C.ColumnId
if I got you right.
精彩评论