Join three tables in SQL Server 2005

So far I have used a join with two tables, but now I want to combine the three tables, as shown in the figure below.

alt text
(source: microsoft.com )

I tried to combine two tables,

SELECT O.OrderID,O.CustID,O.OrderTotal,C.Name from Orders
as O inner join Customers as C on O.CustID=C.CustID 

How to join the third table with this .... Any suggestion ...

+5
source share
3 answers

You do the same with the third table:

SELECT O.OrderID,O.CustID,O.OrderTotal,C.Name, OC.OrderAmount
FROM Orders as O 
INNER JOIN Customers as C 
  ON O.CustID=C.CustID 
INNER JOIN OrderItems as OC
  ON O.OrderID=OC.OrderID 
+11
source

You can simply add another JOIN at the end:

inner join OrderItems as OI ON O.OrderID= OI.OrderID

, ( , , ) . , , , . , , .

+3
    Select Customers.Name
    From OrderItems                            --      (Table 1)
    INNER JOIN Orders                          --      (Table 2)
    ON OrderItems.OrderID = Orders.OrderID
    INNER JOIN Customers                       --      (Table 3)
    ON Orders.CustID = Customers.CustID
    Where Customers.CustID = 2                 --      This will give you the name of the second customer in the third table using JOINS
+3
source

All Articles