1. 程式人生 > >OA專案中關於資料表遞迴查詢

OA專案中關於資料表遞迴查詢

例如知道某員工頂級部門id(企業id)需要查詢所有員工

正常Oracle遞迴

SELECT DISTINCT U.*,UD.ORDER_NUM 
FROM 
TBL_BASE_USER U ,TBL_BASE_USER_DEPT UD,(select*from tbl_base_dept t start with t.dept_id='17052413401200000190' connect by t.dept_parent_id=prior t.dept_id ) A
where          
U.USER_ID = UD.USER_ID 
AND U.USER_TYPE != '1000' 
AND U.USER_TYPE IS NOT NULL  
AND UD.DEPT_ID=A.DEPT_ID

ORDER BY ud.order_num

為了支援Mysql資料庫,部門表中有id_path列,存放所有上級部門id,所以我們只需查詢所有id_path中有當前頂級部門id的部門id

SELECT DISTINCT U.*,UD.ORDER_NUM 
FROM TBL_BASE_USER U ,TBL_BASE_USER_DEPT UD
where          
U.USER_ID = UD.USER_ID 
AND U.USER_TYPE != '1000' 
AND U.USER_TYPE IS NOT NULL  
AND UD.DEPT_ID in(select dept_id from tbl_base_dept where id_path like CONCAT(CONCAT('%,','17052413401200000190'),',%')) 
ORDER BY ud.order_num