Sql server 级联查询 Left Join的应用

LEFT JOIN 关键字语法:

SELECT column_name(s) 
FROM table_name1
LEFT JOIN
table_name2  
ON 
table_name1.column_name=table_name2.column_name

高级应用:

SELECT  	
had.deptTypeId as deptTypeId
,hdt.typeName as deptTypeName
,had.id as deptID
,had.deptName as deptName
,isnull(sg.siteGoalPoint,0) as siteGoalPoint
,isnull(sg.supGoalPoint,0) as supGoalPoint
from
–单位
(select a.deptTypeId,a.id,a.deptName
from HD_ACT_DEPT a
where 1 = 1
and deptTypeId = ‘null’
) as had
LEFT JOIN
–单位类型
HD_DEPT_TYPE hdt
ON had.deptTypeId = hdt.id
LEFT JOIN
–任务完成情况统计表
(select a.siteGoalPoint,a.supGoalPoint,a.deptTypeId,a.deptID
from cms_sup_submit_goal a
where a.year = ‘2013’
) as sg
on had.deptTypeId = sg.deptTypeId and had.id = sg.deptID
ORDER BY had.deptTypeId, had.id
多表查询:
select s.id,s.name,s.sname from
singer s, tb_su su
where s.id=su.sid  and  su.userid=’1′

标签