首页 > 代码库 > 父子级别联合查询

父子级别联合查询

------ 算出本部门和子部门的部门ID
WITH TEMP AS (
SELECT b.DeptID
FROM TIni_UserBasicInfo b left join TSys_Department d on d.DeptID=b.DeptID
WHERE UserID=‘User20161021000001‘
UNION ALL
SELECT D1.DeptID
FROM TSys_Department D1
INNER JOIN TEMP D2 ON D1.PDeptID=D2.DeptID
)
select * from TEMP

 

表结构如下:技术分享

 

父子级别联合查询