2014-04-11 54 views
0

我想寫一個SQL語句:SQL加入使用多個表where子句

Select * FROM table1 
inner join table2 AS t2 
    ON 
inner join table3 AS t3 
    ON 
inner join table4 AS t4 
    ON 
Where FK_Client_ID = 4 

所有的表,客戶端ID共同點。所以不知道在ON上該做什麼。

它會像ON t2.FK_Client_ID = ......不確定。

所以我只想從這些具有FK_Client_ID的表中獲取共同的數據。

回答

0
Select *, 
    (Select FK_Client_ID from table2 where FK_Client_ID = t1.FK_Client_ID )As TID1, 
    (Select FK_Client_ID from table3 where FK_Client_ID = t1.FK_Client_ID )As TID2, 
    (Select FK_Client_ID from table4 where FK_Client_ID = t1.FK_Client_ID )As TID3 
FROM table1 t1 
0

試試這個

Select * FROM table1 t1 
inner join table2 AS t2 
    ON t2.FK_Client_ID = t1.FK_Client_ID 
inner join table3 AS t3 
    ON t3.FK_Client_ID = t1.FK_Client_ID 
inner join table4 AS t4 
    ON t4.FK_Client_ID = t1.FK_Client_ID 
Where t1.FK_Client_ID = 4 
0

如果你的洋場的名字是"FK_Client_ID"和table1的主鍵是"Client_ID"

Select * FROM table1 
inner join table2 AS t2 
    ON t2.FK_Client_ID = table1.Client_ID 
inner join table3 AS t3 
    ON t3.FK_Client_ID = table1.Client_ID 
inner join table4 AS t4 
    ON t4.FK_Client_ID = table1.Client_ID 
Where table1.Client_ID = 4 

由於您使用的內部連接,無論你用table1或其他任何加入他們他們的

+0

以上查詢將導致不明確的列名稱'FK_Client_ID'。 – Kaleel

+0

@Kaleel我編輯了我的答案 – RezaRahmati

+0

您已經在table1中提到了主鍵列'Client_ID',因此'where'條件應該是table1.Client_ID或其他表的別名(t2或t3或t4).FK_Client_ID – Kaleel

0
Select * FROM table1 t1 inner join table2 t2 
    ON t1.FK_Client_ID = t2.FK_Client_ID 
     inner join table3 t3 
    ON t1.FK_Client_ID = t3.FK_Client_ID 
     inner join table4 t4 
    ON t1.FK_Client_ID = t4.FK_Client_ID 
Where t1.FK_Client_ID = 4