濮阳杆衣贸易有限公司

主頁 > 知識庫 > 有關(guān)數(shù)據(jù)庫SQL遞歸查詢在不同數(shù)據(jù)庫中的實現(xiàn)方法

有關(guān)數(shù)據(jù)庫SQL遞歸查詢在不同數(shù)據(jù)庫中的實現(xiàn)方法

熱門標(biāo)簽:南京怎么申請400這種電話 臺灣外呼系統(tǒng)軟件 地圖標(biāo)注可以編輯地名嗎 濮陽清豐400開頭的電話申請 真3地圖標(biāo)注 南通智能外呼系統(tǒng)怎么樣 疫情時期電話機器人 樂昌電話機器人 地圖標(biāo)注跑線下市場

本文給大家介紹有關(guān)數(shù)據(jù)庫SQL遞歸查詢在不同數(shù)據(jù)庫中的實現(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é)點所有下級
DECLARE @ChildTree hierarchyid
SELECT @ChildTree=TId FROM Tree2 WHERE Id=3
SELECT *,TId.GetLevel()AS [level] FROM Tree2 WHERE @ChildTree.IsDescendantOf(TId)=1 --獲取指定的節(jié)點所有上級

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));--上查

補充說明:上面這個方法在下查是沒有問題,但在上查時會出現(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

這樣遞歸查詢就很方便了。

您可能感興趣的文章:
  • 使用PHP數(shù)組實現(xiàn)無限分類,不使用數(shù)據(jù)庫,不使用遞歸.
  • bat/cmd批處理連接SqlServer數(shù)據(jù)庫查詢腳本
  • php簡單實現(xiàn)查詢數(shù)據(jù)庫返回json數(shù)據(jù)

標(biāo)簽:陜西 廣安 福建 河北 通遼 南京 馬鞍山 阿里

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《有關(guān)數(shù)據(jù)庫SQL遞歸查詢在不同數(shù)據(jù)庫中的實現(xiàn)方法》,本文關(guān)鍵詞  有關(guān),數(shù)據(jù)庫,SQL,遞歸,查詢,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《有關(guān)數(shù)據(jù)庫SQL遞歸查詢在不同數(shù)據(jù)庫中的實現(xiàn)方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于有關(guān)數(shù)據(jù)庫SQL遞歸查詢在不同數(shù)據(jù)庫中的實現(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    嵩明县| 庆阳市| 镇康县| 娱乐| 新泰市| 吴川市| 桓台县| 科尔| 武义县| 澄江县| 通化县| 晋江市| 南雄市| 长海县| 兴仁县| 旬阳县| 房产| 元江| 济南市| 会泽县| 贡觉县| 垣曲县| 桓仁| 资阳市| 龙胜| 肇庆市| 双辽市| 噶尔县| 阳春市| 台南市| 灵宝市| 图们市| 南皮县| 崇州市| 陈巴尔虎旗| 兰坪| 绥芬河市| 甘南县| 乳山市| 绥宁县| 五大连池市|