有關(guān)數(shù)據(jù)庫SQL遞歸查詢在不同數(shù)據(jù)庫中的實(shí)現(xiàn)方法
本文給大家介紹有關(guān)數(shù)據(jù)庫SQL遞歸查詢在不同數(shù)據(jù)庫中的實(shí)現(xiàn)方法,具體內(nèi)容請看下文。
比如表結(jié)構(gòu)數(shù)據(jù)如下:
Table:Tree
ID Name ParentId
1 一級 0
2 二級 1
3 三級 2
4 四級 3
SQL SERVER 2005查詢方法:
//上查 with tmpTree as ( select * from Tree where Id=2 union all select p.* from tmpTree inner join Tree p on p.Id=tmpTree.ParentId ) select * from tmpTree //下查 with tmpTree as ( select * from Tree where Id=2 union all select s.* from tmpTree inner join Tree s on s.ParentId=tmpTree.Id ) select * from tmpTree
SQL SERVER 2008及以后版本,還可用如下方法:
增加一列TID,類型設(shè)為:hierarchyid(這個是CLR類型,表示層級),且取消ParentId字段,變成如下:(表名為:Tree2)
TId Id Name
0x 1 一級
0x58 2 二級
0x5B40 3 三級
0x5B5E 4 四級
查詢方法:
SELECT *,TId.GetLevel() as [level] FROM Tree2 --獲取所有層級 DECLARE @ParentTree hierarchyid SELECT @ParentTree=TId FROM Tree2 WHERE Id=2 SELECT *,TId.GetLevel()AS [level] FROM Tree2 WHERE TId.IsDescendantOf(@ParentTree)=1 --獲取指定的節(jié)點(diǎn)所有下級 DECLARE @ChildTree hierarchyid SELECT @ChildTree=TId FROM Tree2 WHERE Id=3 SELECT *,TId.GetLevel()AS [level] FROM Tree2 WHERE @ChildTree.IsDescendantOf(TId)=1 --獲取指定的節(jié)點(diǎn)所有上級
ORACLE中的查詢方法:
SELECT * FROM Tree START WITH Id=2 CONNECT BY PRIOR ID=ParentId --下查 SELECT * FROM Tree START WITH Id=2 CONNECT BY ID= PRIOR ParentId --上查
MYSQL 中的查詢方法:
//定義一個依據(jù)ID查詢所有父ID為這個指定的ID的字符串列表,以逗號分隔
CREATE DEFINER=`root`@`localhost` FUNCTION `getChildLst`(rootId int,direction int) RETURNS varchar(1000) CHARSET utf8
BEGIN
DECLARE sTemp VARCHAR(5000);
DECLARE sTempChd VARCHAR(1000);
SET sTemp = '$';
IF direction=1 THEN
SET sTempChd =cast(rootId as CHAR);
ELSEIF direction=2 THEN
SELECT cast(ParentId as CHAR) into sTempChd FROM Tree WHERE Id=rootId;
END IF;
WHILE sTempChd is not null DO
SET sTemp = concat(sTemp,',',sTempChd);
SELECT group_concat(id) INTO sTempChd FROM Tree where (direction=1 and FIND_IN_SET(ParentId,sTempChd)>0)
or (direction=2 and FIND_IN_SET(Id,sTempChd)>0);
END WHILE;
RETURN sTemp;
END
//查詢方法:
select * from tree where find_in_set(id,getChildLst(1,1));--下查
select * from tree where find_in_set(id,getChildLst(1,2));--上查
補(bǔ)充說明:上面這個方法在下查是沒有問題,但在上查時會出現(xiàn)問題,原因在于我的邏輯寫錯了,存在死循環(huán),現(xiàn)已修正,新的方法如下:
CREATE DEFINER=`root`@`localhost` FUNCTION `getChildLst`(rootId int,direction int) RETURNS varchar(1000) CHARSET utf8
BEGIN
DECLARE sTemp VARCHAR(5000);
DECLARE sTempChd VARCHAR(1000);
SET sTemp = '$';
SET sTempChd =cast(rootId as CHAR);
IF direction=1 THEN
WHILE sTempChd is not null DO
SET sTemp = concat(sTemp,',',sTempChd);
SELECT group_concat(id) INTO sTempChd FROM Tree where FIND_IN_SET(ParentId,sTempChd)>0;
END WHILE;
ELSEIF direction=2 THEN
WHILE sTempChd is not null DO
SET sTemp = concat(sTemp,',',sTempChd);
SELECT group_concat(ParentId) INTO sTempChd FROM Tree where FIND_IN_SET(Id,sTempChd)>0;
END WHILE;
END IF;
RETURN sTemp;
END
這樣遞歸查詢就很方便了。
相關(guān)文章
深入SQLServer中ISNULL與NULLIF的使用詳解
本篇文章是對SQLServer中ISNULL與NULLIF的使用進(jìn)行了詳細(xì)分析介紹,需要的朋友參考下2013-06-06
Win10下安裝Sql Server 2014反復(fù)提示需安裝.NET Framework 3.5 SP1的解決方案
這篇文章主要介紹了Win10下安裝Sql Server 2014反復(fù)提示需安裝.NET Framework 3.5 SP1的解決方案,需要的朋友可以參考下2016-05-05
MS SQL Server排查多列之間的值是否重復(fù)的功能實(shí)現(xiàn)
在日常的應(yīng)用中,排查列重復(fù)記錄是經(jīng)常遇到的一個問題,但某些需求下,需要我們排查一組列之間是否有重復(fù)值的情況,本文給大家介紹了MS SQL Server排查多列之間的值是否重復(fù)的功能實(shí)現(xiàn),需要的朋友可以參考下2024-09-09
一次SQL查詢優(yōu)化原理分析(900W+數(shù)據(jù)從17s到300ms)
本文主要介紹了一次SQL查詢優(yōu)化原理分析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

