2012-04-08 44 views
-1
i have 3 tables (employee,structure,District) 

1_employee table contains empid,workid and actuallwork fks to structid 
2_ structure table contains structid , districtId fk to district 
3_District table contains districtid , districtname 

我想加入3個表中SQLSERVER非常複雜

empid , work 

,actuallwork(從外鍵actulalwork disitrict名)

回答

1

您可以使用相同的表在查詢中多次提供你給每個事件明確的別名。結構和地區被列爲兩次,第一次作爲工作/地區,第二次作爲實際工作/實際地區。在處理對同一個表的多個引用時,這就是您區分列來自哪裏的方式。

select employee.empid, 
     district.districtname, 
     actualdistrict.districtname acturaldistrictname 
    from employee 
    inner join structure work 
     on employee.workid = work.structid 
    inner join district 
     on work.districtfk = district.districtid 
    inner join structure actualwork 
     on employee.actuallworkfk = actualwork.structid 
    inner join district actualdistrict 
     on actualwork.districtfk = actualdistrict.districtid 
+0

非常感謝你非常非常非常 – 2012-04-08 23:31:44