2014-06-28 57 views
0

我在SQL Server中的兩個表爲如何使內部連接兩次單表中的SQL Server

City_Master(
ID bigint, 
Name nvarchar(50) 
) 
Tour_Details(
ID bigint, 
From_City bigint, 
To_City bigint, 
Details nvarchar(max), 
Cost nvarchar(100), 
Tour_Name nvarchar(100), 
Duration nvarchar(max)) 

在Tour_Details表FROM_CITY和TO_CITY列從City_Master表來。我想選擇位於Tour_Details Table的From_City和To_City列中的城市名稱。

我用下面這個

SELECT Tour_Details.ID, Tour_Details.Tour_Name, Tour_Details.From_City, Tour_Details.To_City, Tour_Details.Cost, 
    Tour_Details.Details,Tour_Details.Duration, City_Master.Name as City FROM Tour_Details INNER JOIN 
    City_Master ON Tour_Details.From_City = City_Master.ID ORDER BY Tour_Details.ID DESC 

SQL查詢,但它返回只有一個城市名稱,但也存在於tour_Details表列作爲FROM_CITY,TO_CITY兩市ID。我想這兩個名字cities.Please誰能幫助我

回答

2

試試這個

SELECT Tour_Details.ID, Tour_Details.Tour_Name, Tour_Details.From_City, Tour_Details.To_City, Tour_Details.Cost, 
Tour_Details.Details,Tour_Details.Duration, B.Name as FromCityName, A.Name as ToCityName 
FROM Tour_Details 
INNER JOIN City_Master B ON B.ID = Tour_Details.From_City 
INNER JOIN City_Master A ON A.ID = Tour_Details.To_City 
ORDER BY Tour_Details.ID DESC 
+0

先生,這個查詢有點不對勁,而內部聯接,因爲FROM_CITY和TO_CITY是因爲你已經證明Tour_Details表不City_Master的列在上面的查詢中。 –

+0

@SunnySandeep,對不起...糾正 –

+0

仍然是不正確的。我自己做的正確如下:SELECT Tour_Details.ID,Tour_Details.Tour_Name,Tour_Details.From_City,Tour_Details.To_City,Tour_Details.Cost, Tour_Details.Details,Tour_Details.Duration,B.Name as FromCityName,A.Name as ToCityName FROM Tour_Details INNER JOIN City_Master B開B.ID = Tour_Details.From_City INNER JOIN City_Master A ON A.ID = Tour_Details.To_City ORDER BY Tour_Details.ID DESC –