新聞中心
sql 怎么遞歸查詢的方法:
1.創(chuàng)建測試表,createtabletest_connect(idnumber,p_idnumber);
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供紅寺堡企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為紅寺堡眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。
2.插入測試數(shù)據(jù),
Insertintotest_connectvalues(1,1);
Insertintotest_connectvalues(2,1);
Insertintotest_connectvalues(3,2);
Insertintotest_connectvalues(4,3);
提交;
3.查詢數(shù)據(jù)表的內(nèi)容,選擇*fromtest_connect,
4.執(zhí)行遞歸查詢語句,將答案添加到nocycle元素中,就不會有[ora-01436:CONNECTBYerrorintheuserdata]。執(zhí)行結(jié)果如下:
Select*
來自test_connectt
從id=4開始
由nocyclepriort連接。p_id=t.i.
如何實現(xiàn)數(shù)據(jù)庫SQL遞歸查詢在不同數(shù)據(jù)庫中例子源代碼
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如何實現(xiàn)遞歸查詢檢測
select * from table_name where id in (select id from table connect by prior id = pid start with id = 你想起始的ID);
(select id from table connect by prior id = pid start with id = 你想起始的ID);
這部分就可以遞歸查詢到所有以某一個父ID下的所有的包含父ID在內(nèi)的所有ID!
關(guān)于SQL遞歸查詢問題
我來測一下,等會上傳結(jié)果
你還有一個表沒用到。
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
分享名稱:nosql遞歸的簡單介紹
標題網(wǎng)址:http://ef60e0e.cn/article/hdhpgo.html