當前位置:首頁 » 編程語言 » 多級組織樹查詢sql
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

多級組織樹查詢sql

發布時間: 2022-08-11 15:05:51

『壹』 關於sql server中的樹查詢

就用with遞歸,根節點等級為0,遞歸時每級+1

『貳』 sql組織機構樹查詢

組織等級是四層,應該顯示四列才完整

createtablestat(codevarchar(10),parentvarchar2(10),slevelvarchar(10));
INSERTINTOSTATVALUES('中國','0','國家');
INSERTINTOSTATVALUES('浙江省','中國','省');
INSERTINTOSTATVALUES('江蘇省','中國','省');
INSERTINTOSTATVALUES('杭州市','浙江省','市');
INSERTINTOSTATVALUES('南京市','江蘇省','市');
INSERTINTOSTATVALUES('西湖區','杭州市','縣/區');
INSERTINTOSTATVALUES('玄武區','南京市','縣/區');
SELECTA.PARENT,A.CODE,B.CODE,C.CODE
FROMSTATA,STATB,STATC
WHEREA.CODE=B.PARENT
ANDB.CODE=C.PARENT
ANDA.PARENT='中國';
PARENTCODECODECODE
----------------------------------------
中國浙江省杭州市西湖區
中國江蘇省南京市玄武區

『叄』 SQL 多級查詢(級數不定)

select * from item where itemID=3 union all
select * from item where FDetail=0 and parentID <(select parentID from item where itemID=3)
and substring(Number,1,2) =(select substring(Number,1,2) from item where itemID=3)
and substring(Number,4,2) =(select substring(Number,4,2) from item where itemID=3);

說下思路,先把自己本身一條找出來,然後找他的上級,看你的數據知道parentID 一定小於本身的parentID ,並且是目錄的話FDetail=0,如果是其上級目錄,他們前邊的01.01什麼的都是一樣的,但是現在有個弊端,就是查詢前,要確定這個itemID=3的是屬於第幾級實體,然後才能採用後邊用多少個substring,另一個表itemID=3的條件沒用,其實就是一個嵌套,你自己寫里邊吧

『肆』 SQL樹形層級查詢

你好的!
oracle 的start with connect by
別的資料庫用cte 遞歸都能達到你要的效果!
望採納~

『伍』 SQL2008怎麼實現樹形數據查詢,語句該怎麼寫

select 部門名稱, (select 部門id from table where 上級部門id=1) from table where 部門名稱=總經辦

『陸』 求教oracle怎麼用一個SQL查詢多層樹形結構

select * from 表 m start with m.id=1 connect by m.parent=prior m.id;

『柒』 高難!小白求救,sql 每層有一到三個下級如何查詢子樹

類似這種語法,你可以參考一下:

WITH leaderCTE as(
SELECT id, name,manager
FROM [lulinghao].[dbo].[YGB]
WHERE name = @name

UNION ALL

SELECT ygb.id, ygb.name,ygb.manager
FROM [lulinghao].[dbo].[YGB] as ygb
inner join leaderCTE lce on lce.manager = ygb.id
)
SELECT case when [id]=100 then 1 when [id]=101 OR [id]=102 then 2 else 3 end as leaderlevel,
name as leadername
FROM leaderCTE

『捌』 關於SQL查詢樹結構數據的語句

方法1:
是否可以有代表層次的欄位,若有,直接根據層次欄位查詢;
方法2:
是否存在以下假設:
非父級的dept_uid的 不可能 在 parent_uid 中出現。
如果假設成立,在查出的所有dept_uid中去掉,所有在在 parent_uid 中出現id,
剩下的就是你要的了。

『玖』 sql 查詢樹形數據。

如果樹的層數固定就可以用語句查詢,但效率比較低。例如你說的三層:
select id,v2.name+name from t1 inner join
(select id,v1.name+name as name from t1 inner join
(select id,name from t1 where parentid = 0) v1 on t1.parentid = v1.id) v2 on t1.parentid = v2.id

『拾』 sql怎麼實現樹查詢

表格式如下:

cid pid cname
1 0 董事長
2 1 CEO
3 2 銷售經理
4 2 IT經理
5 2 運營經理
6 3 銷售主管
7 4 IT主管
8 5 運營主管
9 6 業務員
10 7 程序員
11 8 運營員

create function get_detail(
@id int
)returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.cid,@l
from table_2 a,@re b
where a.pid=b.id and b.level=@l-1
end
return
end
go

--調用(查詢所有的子)
select a.*,level=b.level from table_2 a,get_detail(2)b where a.cid=b.id