2011-12-02 94 views
0

在SQL Server中,我加入瞭如下所示的三個表。在sql server中連接表?

select t1.empid, t2.sales, t3.date 
from table1 t1 
left outer join table2 t2 on t1.empid = t2.empid 
left outer join table3 t3 on t1.empid = t2.empid 
    and t2.id= t3.id 

這是正確的,我用and的條件,謝謝。

,如果我寫的PROC:我正在使用joing左外部表連接

select wrh.empid 
from Tbl_F_Weekly_Report_Header WRH 
left outer join Tbl_Emp_Master_M EM on wrh.EmpId =em.EmpId 
LEFT outer join Tbl_F_Emp_Position_M EPS on WRH.PositionCode = EPS.PositionCode 
where EM [Tbl_Emp_Master_M] doesnot contain Positioncode 

是正確

+0

我不明白你的'WHERE'條款。你能提供樣本數據和預期結果嗎? –

+0

ya mr.adma wenger我不得不說的是在Tbl_emp_master_m我沒有positioncode它不是在哪裏條件。 –

+0

謝謝你清理那個。您的查詢是正確的。 –

回答

2

LEFT OUTER JOIN上表3是不正確。你只需要:

SELECT t1.empid, t2.sales, t3.date 
FROM table1 AS t1 
LEFT OUTER JOIN table2 AS t2 ON t1.empid = t2.empid 
LEFT OUTER JOIN table3 AS t3 ON t2.id = t3.id 

,因爲你已經在第一LEFT OUTER JOIN

你更新的查詢指定上述t1.empid = t2.empid

SELECT wrh.empid 
FROM Tbl_F_Weekly_Report_Header AS wrh 
LEFT OUTER JOIN Tbl_Emp_Master_M AS em ON wrh.EmpId = em.EmpId  
LEFT OUTER JOIN Tbl_F_Emp_Position_M AS eps ON wrh.PositionCode = eps.PositionCode 

看起來不錯,只要確定你包括FROM(最初失蹤從你的問題)

+0

請檢查我的另一個查詢Mr> Adm Wenger –

+0

@Suryasasidhar我已更新我的答案。你的第二個查詢看起來不錯 –

+0

tahnk you先生adam溫格 –

0

你剛剛錯過選擇>選擇和t3左外連接。您必須使用SQL Server Management Studio來測試您的查詢。

select 
t1.empid, 
t2.sales, 
t3.date 
from table1 t1 
left outer join table2 t2 on t1.empid=t2.empid 
left outer join table3 t3 on t2.id= t3.id 

問候

+0

請檢查我的另一個查詢先生BizApps –

0

是你的SQL語句是正確的。但你可以試試這個:

select t1.empid, t2.sales, t3.date 
from table1 t1 
left outer join table2 t2 on t1.empid = t2.empid 
inner join table3 t3 on t2.empid = t3.empid 

select t1.empid, t2.sales, t3.date 
from table1 t1 
left outer join table2 t2 on t1.empid = t2.empid 
left outer join table3 t3 on t2.empid = t3.empid 
+0

請檢查我的另一個查詢先生Rj1516 –