2013-03-12 34 views
2

我想連接3個表與一個內部連接。表prod_num和usrs由usr_Id來解除,而usrs和usr_Data由usr_Num相關。因此,表1和表3中不之間,所以我想realte與表3這種連接1和2的結果相關的是什麼我嘗試做的事:加入SQL Server 2008中的3個表

USE Puzzle 
SELECT a.Name, a.Id, b.Id, c.Email 
FROM prod_num a INNER JOIN usrs b 
ON a.usr_Num = b.usr_Num<br>FROM usrs b INNER JOIN usr_Data c 
ON b.usr_Id = c.usr_Id 

我怎樣才能做一個從結果加入以前加入?

回答

4

您應該只使用一個FROM關鍵字,因爲JOIN s爲的FROM子句的一部分,

SELECT a.Name, 
     a.Id, 
     b.Id, 
     c.Email 
FROM prod_num a 
     INNER JOIN usrs b 
      ON a.usr_Num = b.usr_Num 
     INNER JOIN usr_Data c 
      ON b.usr_Id = c.usr_Id 

爲了進一步獲得更多的知識有關加入,請訪問以下鏈接:

3

試試這個:

USE Puzzle 
SELECT a.Name, a.Id, b.Id, c.Email 
FROM prod_num a 
INNER JOIN usrs b ON a.usr_Num = b.usr_Num 
INNER JOIN usr_Data c ON b.usr_Id = c.usr_Id 

你有你的JOIN語句之間的額外FROM條款。

2

你可以這樣做:

USE Puzzle 
SELECT 
    a.Name, 
    a.Id, 
    b.Id, 
    c.Email 
FROM prod_num a 
INNER JOIN usrs b ON a.usr_Num = b.usr_Num 
INNER JOIN usr_Data c ON b.usr_Id = c.usr_Id