有关数据库SQL递归查询在不同数据库中的实现方法

所属分类: 数据库 / MsSql 阅读数: 1700
收藏 0 赞 0 分享

本文给大家介绍有关数据库SQL递归查询在不同数据库中的实现方法,具体内容请看下文。

比如表结构数据如下:

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,类型设为: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 --获取指定的节点所有下级
DECLARE @ChildTree hierarchyid
SELECT @ChildTree=TId FROM Tree2 WHERE Id=3
SELECT *,TId.GetLevel()AS [level] FROM Tree2 WHERE @ChildTree.IsDescendantOf(TId)=1 --获取指定的节点所有上级

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 中的查询方法:

//定义一个依据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));--上查

补充说明:上面这个方法在下查是没有问题,但在上查时会出现问题,原因在于我的逻辑写错了,存在死循环,现已修正,新的方法如下:

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

这样递归查询就很方便了。

更多精彩内容其他人还在看

SQL Substring提取部分字符串

SQL 中的 substring 函数是用来抓出一个栏位资料中的其中一部分。这个函数的名称在不同的资料库中不完全一样
收藏 0 赞 0 分享

用户"sa"登陆失败 SQLServer 错误18456的解决方法

sqlserver下用sa登录提示18456错误的解决方法。
收藏 0 赞 0 分享

drop,truncate与delete的区别

这里说的delete是指不带where子句的delete语句
收藏 0 赞 0 分享

SQL语句查询数据库中重复记录的个数

一个sql语句:一个表test有四个字段id,a,b,c,如果表中的记录有三个字段a,b,c都相等,则说明这条记录是相同的,求相同的记录的个数 。
收藏 0 赞 0 分享

SQL 导入导出Excel数据的语句

从Excel文件中,导入数据到SQL数据库中,很简单
收藏 0 赞 0 分享

可以获取客户端的IP地址的sql语句

利用SQL语句得到客户端的IP地址的代码
收藏 0 赞 0 分享

SQL 根据汉字获取全拼的代码

SQL 根据汉字获取全拼(有些字还没有添加上去,请自已加上去,涂聚文注)
收藏 0 赞 0 分享

sqlserver exists,not exists的用法

exists,not exists的使用方法示例,需要的朋友可以参考下。
收藏 0 赞 0 分享

sqlserver substring函数使用方法小结

在操作sqlserver时候用到了substring函数,特整理一些实例,需要的朋友可以参考下。
收藏 0 赞 0 分享

sql 判断数据库,表,存储过程等是否存在的代码

sql下用了判断各种资源是否存在的代码,很实用。需要的朋友可以参考下。
收藏 0 赞 0 分享
查看更多