有关数据库SQL递归查询在不同数据库中的实现方法
数据库 / 管理员 发布于 6年前 158
本文给大家介绍有关数据库SQL递归查询在不同数据库中的实现方法,具体内容请看下文。
比如表结构数据如下:
Table:Tree
ID Name ParentId
1 一级 0
2 二级 1
3 三级 2
4 四级 3
SQL SERVER 2005查询方法:
//上查with tmpTreeas( 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 tmpTreeas( 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 hierarchyidSELECT @ParentTree=TId FROM Tree2 WHERE Id=2SELECT *,TId.GetLevel()AS [level] FROM Tree2 WHERE TId.IsDescendantOf(@ParentTree)=1 --获取指定的节点所有下级DECLARE @ChildTree hierarchyidSELECT @ChildTree=TId FROM Tree2 WHERE Id=3SELECT *,TId.GetLevel()AS [level] FROM Tree2 WHERE @ChildTree.IsDescendantOf(TId)=1 --获取指定的节点所有上级
ORACLE中的查询方法:
SELECT *FROM TreeSTART WITH Id=2CONNECT BY PRIOR ID=ParentId --下查SELECT *FROM TreeSTART WITH Id=2CONNECT BY ID= PRIOR ParentId --上查
MYSQL 中的查询方法:
//定义一个依据ID查询所有父ID为这个指定的ID的字符串列表,以逗号分隔CREATE DEFINER=`root`@`localhost` FUNCTION `getChildLst`(rootId int,direction int) RETURNS varchar(1000) CHARSET utf8BEGIN 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 utf8BEGIN 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
这样递归查询就很方便了。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号