Sam OZ
asked on
Sql Server query to left join
I have an Sql Server 2016 Database with tables TB_Cable, TB_Item, TB_Connection
Samples data below ( There are more fileds on each table)
TB_Cable
Cable_ID Cable_Num CableLoc
C1 Cable1 123,St
C2 Cable2 345,St
C3 Cable3 456,St
TB_Items
Item_ID ItemName
I1 Item1
I2 Item2
I3 Item3
TB_Connection
Cable_ID Item_ID
C1 I1
C2 I3
I am looking for the query to give result as follows
Result of query ( Has all cable . Lists Items when Cable is connected. For other cables just "Vacant" is displayed for Item
Cable&Connection
-----------------------
CableNum CableLoc ItemName
Cable1 123,St Item1
Cable2 345,st Item2
Cable3 456,St Vacant
Samples data below ( There are more fileds on each table)
TB_Cable
Cable_ID Cable_Num CableLoc
C1 Cable1 123,St
C2 Cable2 345,St
C3 Cable3 456,St
TB_Items
Item_ID ItemName
I1 Item1
I2 Item2
I3 Item3
TB_Connection
Cable_ID Item_ID
C1 I1
C2 I3
I am looking for the query to give result as follows
Result of query ( Has all cable . Lists Items when Cable is connected. For other cables just "Vacant" is displayed for Item
Cable&Connection
-----------------------
CableNum CableLoc ItemName
Cable1 123,St Item1
Cable2 345,st Item2
Cable3 456,St Vacant
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
IN the case of
LEFT JOIN @TB_Connection c ON c.Cable_ID = b.Cable_ID
can I add c.Term_ID = b.Term_ID
The left join result need to give result only if both Cable_ID and Term_ID conditios are met