當前位置:首頁 » 操作系統 » 遞歸查詢資料庫

遞歸查詢資料庫

發布時間: 2023-04-19 02:54:30

資料庫語句的遞歸查詢求助

應該是這樣:

with qry as (select user_id,parent_id from tab where user_id = 32
union all
select tab.user_id,tab.parent_id from tab,qry
where tab.parent_id = qry.id)
select * from qry ;

我用mysql5.0.22,不支持上述語法,oracle就可以(sqlserver應該也可以):
create table tab1(user_id int, parent_id int);
insert into tab1 values(1,null);
insert into tab1 values(32,1);
insert into tab1 values(101,32);
insert into tab1 values(102,32);
insert into tab1 values(201,101);
insert into tab1 values(202,101);
insert into tab1 values(203,102);
insert into tab1 values(204,102);
select * from tab1;
with qry(user_id,parent_id) as (select user_id,parent_id from tab1 where user_id = 32
union all
select tab1.user_id,tab1.parent_id from tab1,qry
where tab1.parent_id = qry.user_id
)
select * from qry;

所以,mysql沒有辦法了,只有寫函數,用循環來實現了。

② SQL資料庫實現遞歸查詢的幾種代碼方法

SQL 資料庫 實現遞歸查詢的幾種代碼方法 表結構

ProctCategory

CategoryID Level ParentCategoryID

數據

T SQL

WITH CategoryTemp(CategoryID ParentCategoryID) 臨時表用來保存查到的Category

(

SELECT CategoryID ParentCategoryID FROM ProctCategory WHERE ParentCategoryID<= 將所有的第一層查出來作為初始數據 需要查第幾層或者哪個ParentCategoryID下面所有的 N層 把ParentCategoryID賦相關的值即可

UNION ALL 查詢N層

SELECT pc CategoryID ParentCategoryID FROM ProctCategory pc

LEFT JOIN CategoryTemp ct ON pc ParentCategoryID=ct CategoryID

WHERE ParentCategoryID> 因為第一層前面已經查出來了 所以這里把第一層篩選掉

)

SELECT CategoryID ParentCategoryID FROM CategoryTemp

結果

裂鍵檔

如果把ParentCategoryID賦為 結果則為

實例

ID是否為部門 部門名 上級ID y 部門 y 部門 n 張三 n 李二 y 部門 n 王五 y 部門3亮賀 n 小三 我想找詢 ID 值為 下級的所有人員包括下級部門的所有人員

創建查詢函數 create function f_id( @id int 要查詢的id )returns @re table(id int level int) as begin declare @l int set @l= insert @re select id @l from 表 where 上級id=@id while @@rowcount> begin set @l=@l+ insert @re select a id @l from 表 a join @re b on a 上級id=b id and b level=@l end return end go

調用函數進行查詢 select a * from 表 a join f_id( ) b on a id=b id

聯合查詢

測試數據 create table 表(ID int 是否為部門 char( ) 部門名 varchar( ) 上級ID int) insert 表 select y 部門 union all select y 部門 union all肆亂 select n 張三 union all select n 李二 union all select y 部門 union all select n 王五 union all select y 部門 union all select n 小三 go

創建查詢函數 create function f_id( @id int 要查詢的id )returns @re table(id int level int) as begin declare @l int set @l= insert @re select id @l from 表 where 上級id=@id while @@rowcount> begin set @l=@l+ insert @re select a id @l from 表 a join @re b on a 上級id=b id and b level=@l end return end go

調用函數進行查詢 select a * from 表 a join f_id( ) b on a id=b id go

刪除測試 drop table 表 drop function f_id

/* 測試結果

ID 是否為部門 部門名 上級ID n 小三

lishixin/Article/program/MySQL/201311/29557

③ Oracle資料庫用sql語句做遞歸查詢的效率高嗎

隨便哪個系統,效率都不會高。
盡量避免:
1、改用集合查詢
2、表結構設計時優化。比如:Code採用層次表達:01.02.aa

④ 如何實現資料庫SQL遞歸查詢在不同資料庫中例子源代碼

sql 遞歸查詢的方法:
方法一:T-SQL遞歸查詢
with Dep as
(
select Id,DeptCode,DeptName from Department where Id=1
union all
select d.Id,d.DeptCode,d.DeptName from Dep
inner join Department d on dep.Id = d.ParentDeptId
)
select * from Dep
方法二:PL/SQL遞歸查詢
select Id,DeptCode,DeptName
from Department
start with Id = 1
connect by prior Id = ParentDeptId;

⑤ 關於SQL遞歸查詢問題

我來測一下,等會上傳結果

你還有一個表沒用到。
WITH cte AS (
SELECT RegionID,RegionName,RegionPID FROM [tbRegionTree] WHERE regionPID='01'
UNION ALL
SELECT d.RegionID,d.RegionName,d.RegionPID FROM cte c inner JOIN [tbRegionTree] d ON d.regionPID=c.RegionID

)
SELECT * FROM cte
上面是找到 01 中國的。

下面為非中國的。 條件上處理下
WITH cte AS (
SELECT RegionID,RegionName,RegionPID FROM [tbRegionTree] WHERE regionPID NOT LIKE '01%'
UNION ALL
SELECT d.RegionID,d.RegionName,d.RegionPID FROM cte c inner JOIN [tbRegionTree] d ON d.regionPID=c.RegionID

)
SELECT * FROM cte

java 遞歸查詢資料庫

這個就是個樹形結構,數據結構里的東西。查詢後構建一個樹就行了。稍後給你一個代碼。

熱點內容
內置存儲卡可以拆嗎 發布:2025-05-18 04:16:35 瀏覽:333
編譯原理課時設置 發布:2025-05-18 04:13:28 瀏覽:374
linux中進入ip地址伺服器 發布:2025-05-18 04:11:21 瀏覽:610
java用什麼軟體寫 發布:2025-05-18 03:56:19 瀏覽:31
linux配置vim編譯c 發布:2025-05-18 03:55:07 瀏覽:106
砸百鬼腳本 發布:2025-05-18 03:53:34 瀏覽:940
安卓手機如何拍視頻和蘋果一樣 發布:2025-05-18 03:40:47 瀏覽:737
為什麼安卓手機連不上蘋果7熱點 發布:2025-05-18 03:40:13 瀏覽:801
網卡訪問 發布:2025-05-18 03:35:04 瀏覽:507
接收和發送伺服器地址 發布:2025-05-18 03:33:48 瀏覽:370