1、oracle
以scott.emp表舉例:empno是人員編號,mgr是上級領導(也就是上級人員編碼)
(1)、從上到下查詢
--該查詢查詢員工jones下屬所有的員工
select emp.*
from emp
start with ename='jones'
connect by mgr=prior empno;
7566 jones manager 7839 02-4月 -81 2975 (null) 20
7788 scott analyst 7566 19-4月 -87 3000 (null) 20
7876 adams clerk 7788 23-5月 -87 1100 (null) 20
7902 ford analyst 7566 03-12月-81 3000 (null) 20
7369 smith clerk 7902 17-12月-80 800 (null) 20
(2)、從下到上查詢
--該查詢查詢員工jones上面的所有的員工
select emp.*
from emp
start with ename='jones'
connect by prior mgr=empno;
7566 jones manager 7839 02-4月 -81 2975 (null) 20
7839 king president (null) 17-11月-81 5000 (null) 10
(3)、中見到兩邊查詢
--該查詢以員工jones為準查詢上下級所有員工
select emp.*
from emp
start with ename='jones'
connect by mgr=prior empno
union
select emp.*
from emp
start with ename='jones'
connect by prior mgr=empno;
select
tab_temp.id,tab_temp.fid,tab_temp.user_id,tab_temp.user_name,ltrim(sys_connect_by_path(nchr(tab_temp.neworder),','),',') neworder
from
( select
g_dept.id,g_dept.fid,g_dept.user_id,g_users.uname user_name,level lev,g_users.utype,
(row_number () over (partition by g_dept.fid order by g_dept.shorder)) neworder
from g_dept
inner join g_users on g_users.id=g_dept.user_id
where 1=1 and g_users.status>-1 and g_users.isnative=1
start with g_dept.fid=0
connect by prior g_dept.id=g_dept.fid
) tab_temp
where 1=1 and tab_temp.utype in(0,9)
start with tab_temp.fid=0
connect by prior tab_temp.id=tab_temp.fid
order by neworder;
select g_users.id,g_users.uname,g_dept.fid,g_dept.id,
sys_connect_by_path(g_dept.shorder,',') bb,
level lv
from (
select g_dept.id,g_dept.fid,g_dept.user_id,g_dept.ismain,
lpad(row_number() over(partition by g_dept.fid order by g_dept.shorder),3,'0') shorder
from g_dept
where 1=1
) g_dept
inner join g_users on g_users.id=g_dept.user_id
where 1=1and g_users.status>-1 and g_users.utype=0 and g_dept.ismain=1
start with g_dept.user_id in(
select tab_inner1.user_id from g_dept tab_inner1
inner join g_dept tab_inner2 on tab_inner2.id=tab_inner1.fid
where tab_inner2.user_id=(select id from g_users where utype=8 and status>-1 and isnative=1)
)connect by prior g_dept.id=g_dept.fid
order by bb;
2、sql server
把emp表的資料遷移到sql server上面來
(1)、從下級到上級取資料
--以員工jones為準獲取上級的所有員工
with orgpath(empno,mgr)as(
select empno,mgr from emp where ename='jones'
union all
select emp.empno,emp.mgr
from emp
inner join orgpath on emp.empno=orgpath.mgr
)select emp.*
from emp
inner join orgpath on orgpath.empno=emp.empno;
(資料和oracle上的一樣)
(2)、從上級到下級取資料
--以員工jones為準獲取下級的所有員工
with orgpath(empno,mgr)as(
select empno,mgr from emp where ename='jones'
union all
select emp.empno,emp.mgr
from emp
inner join orgpath on emp.mgr=orgpath.empno
)select emp.*
from emp
inner join orgpath on orgpath.empno=emp.empno;
(資料與oracle的一樣)
(3)、從中間到兩邊取資料
(資料與oracle資料一樣)--以員工jones為準獲取上下級的所有員工
with
temppath(empno,mgr)as(
select empno,mgr from emp where ename='jones'
union all
select emp.empno,emp.mgr
from emp
inner join temppath on emp.mgr=temppath.empno
),orgpath(empno,mgr)as(
select empno,mgr from emp where ename='jones'
union all
select emp.empno,emp.mgr
from emp
inner join orgpath on emp.empno=orgpath.mgr
)select emp.* from emp
where 1=1
and (
emp.empno in(select empno from temppath)
or emp.empno in(select empno from orgpath)
);
其他資料庫以後更新。。。。
SQLserver資料庫中關於遞迴的查詢使用
度量快速開發平台資料庫支援sqlserver資料庫,我們之前習慣的oracle遞迴查詢用的 start with dept id 1000 connect by prior dept id upper id的方式就不靈了。比如我們的組織機構裡面有很多下級機構及部門,要查詢出登入人所在的機構,並列出該...
資料庫遞迴查詢
今天工作看同事 用到了start with.connect by prior,不知道什麼意思,查詢了一些資料,以下是比較好理解的。oracle中的select語句可以用start with.connect by prior子句實現遞迴查詢,connect by 是結構化查詢中用到的,其基本語法是 s...
資料庫中的遞迴查詢
最近工作中用到了遞迴查詢,順便做下備忘 1.sql2005中的cte查詢 with cte aaaaa,bbbbb,ccccc as 查詢id為411321150的資料 select id as aaaaa,parent as bbbbb,name as ccccc from regioninfo ...