2011-12-02 112 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 –