日期:2014-05-18  浏览次数:20489 次

视图中的查询语句 请教高手急急急急!!!!!!!
5张表连表查询查询出所有列
table1 userid
table2 jobid userid  
table3 userid jobid depid 
table4 depid  
table5 sapid

要求:查询出所有能够达到匹配的数据,不能匹配的显示为空也要查出来

------解决方案--------------------
SQL code

select * 
from table1 a 
left join table2 b on a.userid=b.userid   
left join table3 c on a.userid=c.userid  
left join table4 d on c.depid=d.depid   
left join table5 e on a.userid=e.userid

------解决方案--------------------
SQL code

select *from(
select *from(
select * from table1  
union all
select * from table2 
)a left join
table2 on a.userid=table2.userid) b left join
(select table4 .deptid,table3 * from table4  
left join table3  on table4 .deptid=table3.userid)c
on b.userid=c.userid

------解决方案--------------------
Select *
From Table1 Left Join Table2 On (Table1.userid = Table2.userid)
Left Join Table3 On (Table2.jobid = Table3.jobid And Table1.userid = Table3.userid)
Left Join Table4 On (Table3.jobid = Table4.jobid And Table3.userid = Table4.userid And Table3.depid = Table4.depid)
Left Join Table5 On (Table1.userid = Table5.userid)