有关数据库SQL递归查询在不同数据库中的实现方法
作者:梦在旅途 时间:2024-01-28 00:40:15
本文给大家介绍有关数据库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)
TIdIdName
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,递归,查询
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP程序员面试题
2011-09-15 20:51:20
vuejs实现ready函数加载完之后执行某个函数的方法
2024-05-29 22:48:43
python修改txt文件中的某一项方法
2021-02-08 14:26:40
![](https://img.aspxhome.com/file/2023/8/92088_0s.jpg)
Pytorch实现List Tensor转Tensor,reshape拼接等操作
2021-06-06 19:58:51
![](https://img.aspxhome.com/file/2023/6/80786_0s.png)
如何查看SQLSERVER中某个查询用了多少TempDB空间
2024-01-16 06:40:21
![](https://img.aspxhome.com/file/2023/6/94426_0s.png)
MySQL数据库索引order by排序精讲
2024-01-17 09:33:43
![](https://img.aspxhome.com/file/2023/5/69755_0s.png)
利用pandas按日期做分组运算的操作
2021-03-06 09:16:17
基于MATLAB和Python实现MFCC特征参数提取
2023-01-29 07:37:53
![](https://img.aspxhome.com/file/2023/1/95731_0s.png)
解决Python正则表达式匹配反斜杠''\\''问题
2022-06-19 09:10:55
linux下mysql5.7.19(tar.gz)安装图文教程
2024-01-19 07:07:39
![](https://img.aspxhome.com/file/2023/8/105218_0s.jpg)
JS实现根据数组对象的某一属性排序操作示例
2024-04-19 10:14:50
![](https://img.aspxhome.com/file/2023/5/135875_0s.png)
nodejs中的异步编程知识点详解
2024-05-08 10:10:55
oracle SQL解析步骤小结
2009-09-30 10:46:00
记录模型训练时loss值的变化情况
2022-03-22 23:39:48
![](https://img.aspxhome.com/file/2023/9/105249_0s.jpg)
举例讲解Python的Tornado框架实现数据可视化的教程
2022-01-16 23:56:52
![](https://img.aspxhome.com/file/2023/1/123811_0s.png)
GO的基础知识扫盲注意事项
2023-07-12 21:30:26
利用rest framework搭建Django API过程解析
2021-08-09 21:33:46
![](https://img.aspxhome.com/file/2023/0/109600_0s.png)
python检查目录文件权限并修改目录文件权限的操作
2022-06-26 04:13:31
selenium切换标签页解决get超时问题的完整代码
2023-08-26 09:41:25
![](https://img.aspxhome.com/file/2023/7/118427_0s.gif)
Django项目搭建之实现简单的API访问
2021-05-20 23:21:33
![](https://img.aspxhome.com/file/2023/2/101572_0s.jpg)