简体   繁体   中英

Joining third table in SQL

In the below query, I am trying to SELECT Destination_City.Name_ , and to do so need to join Destination_City.Destination_City_ID with Contract_Items.Destination_City_ID , however the latter table is already inner joined with the Contract table.

SELECT 
    contract.Description,
    ---Destination_City.Name_,
    contract_type.type AS 'Contract Type',
    contract.Tour_Summary AS 'Tour Name',
    employee.First_Name AS 'First Name',
    employee.Last_name AS 'Last Name',
    Currency_.Currency_Name AS 'Currency',
    contract_Items.twin AS 'Twin Rate',
    contract_items.Item_Rate AS 'Rate'

FROM 
    Contract
        INNER JOIN Employee 
            ON Contract.Contracted_By_Employee_Id = Employee.Employee_Id
        INNER JOIN Contract_Items
            ON Contract.Contract_ID = Contract_Items.Contract_ID
        INNER JOIN Currency_
            ON Contract.Currency_Id = Currency_.Currency_Id
        INNER JOIN Contract_Type
            ON Contract.Contract_Type_Id = contract_type.Contract_Type_Id

Essentially Table 2 needs to be joined to Table 3, but Table 2 is already joined to Table 1. I have tried a couple of different approaches but both have thrown errors, and I am not sure how to write up this query.

It should be no problem to get your Name_ column with joining the DESTINATION_CITY table:

SELECT 
    contract.Description,
    Destination_City.Name_,
    contract_type.type AS 'Contract Type',
    contract.Tour_Summary AS 'Tour Name',
    employee.First_Name AS 'First Name',
    employee.Last_name AS 'Last Name',
    Currency_.Currency_Name AS 'Currency',
    contract_Items.twin AS 'Twin Rate',
    contract_items.Item_Rate AS 'Rate'
FROM 
    Contract
        INNER JOIN Employee 
            ON Contract.Contracted_By_Employee_Id = Employee.Employee_Id
        INNER JOIN Contract_Items
            ON Contract.Contract_ID = Contract_Items.Contract_ID
        INNER JOIN Currency_
            ON Contract.Currency_Id = Currency_.Currency_Id
        INNER JOIN Contract_Type
            ON Contract.Contract_Type_Id = contract_type.Contract_Type_Id
        INNER JOIN Destination_City
            ON Destination_City.Destination_City_ID = Contract_Items.Destination_City_ID

Please note that the order of the join operations is irrelevant, if you have inner joins only. It's only required that one of the tables is already joined before.

this?

FROM Contract
    INNER JOIN Employee 
        ON Contract.Contracted_By_Employee_Id = Employee.Employee_Id
    INNER JOIN Contract_Items
        ON Contract.Contract_ID = Contract_Items.Contract_ID
    INNER JOIN Destination_City ON
        Destination_City.Destination_City_ID = Contract_Items.Destination_City_ID
    INNER JOIN Currency_
        ON Contract.Currency_Id = Currency_.Currency_Id
    INNER JOIN Contract_Type
        ON Contract.Contract_Type_Id = contract_type.Contract_Type_Id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM