当前位置:首页 » 操作系统 » 递归查询数据库

递归查询数据库

发布时间: 2023-04-19 02:54:30

数据库语句的递归查询求助

应该是这样:

with qry as (select user_id,parent_id from tab where user_id = 32
union all
select tab.user_id,tab.parent_id from tab,qry
where tab.parent_id = qry.id)
select * from qry ;

我用mysql5.0.22,不支持上述语法,oracle就可以(sqlserver应该也可以):
create table tab1(user_id int, parent_id int);
insert into tab1 values(1,null);
insert into tab1 values(32,1);
insert into tab1 values(101,32);
insert into tab1 values(102,32);
insert into tab1 values(201,101);
insert into tab1 values(202,101);
insert into tab1 values(203,102);
insert into tab1 values(204,102);
select * from tab1;
with qry(user_id,parent_id) as (select user_id,parent_id from tab1 where user_id = 32
union all
select tab1.user_id,tab1.parent_id from tab1,qry
where tab1.parent_id = qry.user_id
)
select * from qry;

所以,mysql没有办法了,只有写函数,用循环来实现了。

② SQL数据库实现递归查询的几种代码方法

SQL 数据库 实现递归查询的几种代码方法 表结构

ProctCategory

CategoryID Level ParentCategoryID

数据

T SQL

WITH CategoryTemp(CategoryID ParentCategoryID) 临时表用来保存查到的Category

(

SELECT CategoryID ParentCategoryID FROM ProctCategory WHERE ParentCategoryID<= 将所有的第一层查出来作为初始数据 需要查第几层或者哪个ParentCategoryID下面所有的 N层 把ParentCategoryID赋相关的值即可

UNION ALL 查询N层

SELECT pc CategoryID ParentCategoryID FROM ProctCategory pc

LEFT JOIN CategoryTemp ct ON pc ParentCategoryID=ct CategoryID

WHERE ParentCategoryID> 因为第一层前面已经查出来了 所以这里把第一层筛选掉

)

SELECT CategoryID ParentCategoryID FROM CategoryTemp

结果

裂键档

如果把ParentCategoryID赋为 结果则为

实例

ID是否为部门 部门名 上级ID y 部门 y 部门 n 张三 n 李二 y 部门 n 王五 y 部门3亮贺 n 小三 我想找询 ID 值为 下级的所有人员包括下级部门的所有人员

创建查询函数 create function f_id( @id int 要查询的id )returns @re table(id int level int) as begin declare @l int set @l= insert @re select id @l from 表 where 上级id=@id while @@rowcount> begin set @l=@l+ insert @re select a id @l from 表 a join @re b on a 上级id=b id and b level=@l end return end go

调用函数进行查询 select a * from 表 a join f_id( ) b on a id=b id

联合查询

测试数据 create table 表(ID int 是否为部门 char( ) 部门名 varchar( ) 上级ID int) insert 表 select y 部门 union all select y 部门 union all肆乱 select n 张三 union all select n 李二 union all select y 部门 union all select n 王五 union all select y 部门 union all select n 小三 go

创建查询函数 create function f_id( @id int 要查询的id )returns @re table(id int level int) as begin declare @l int set @l= insert @re select id @l from 表 where 上级id=@id while @@rowcount> begin set @l=@l+ insert @re select a id @l from 表 a join @re b on a 上级id=b id and b level=@l end return end go

调用函数进行查询 select a * from 表 a join f_id( ) b on a id=b id go

删除测试 drop table 表 drop function f_id

/* 测试结果

ID 是否为部门 部门名 上级ID n 小三

lishixin/Article/program/MySQL/201311/29557

③ Oracle数据库用sql语句做递归查询的效率高吗

随便哪个系统,效率都不会高。
尽量避免:
1、改用集合查询
2、表结构设计时优化。比如:Code采用层次表达:01.02.aa

④ 如何实现数据库SQL递归查询在不同数据库中例子源代码

sql 递归查询的方法:
方法一:T-SQL递归查询
with Dep as
(
select Id,DeptCode,DeptName from Department where Id=1
union all
select d.Id,d.DeptCode,d.DeptName from Dep
inner join Department d on dep.Id = d.ParentDeptId
)
select * from Dep
方法二:PL/SQL递归查询
select Id,DeptCode,DeptName
from Department
start with Id = 1
connect by prior Id = ParentDeptId;

⑤ 关于SQL递归查询问题

我来测一下,等会上传结果

你还有一个表没用到。
WITH cte AS (
SELECT RegionID,RegionName,RegionPID FROM [tbRegionTree] WHERE regionPID='01'
UNION ALL
SELECT d.RegionID,d.RegionName,d.RegionPID FROM cte c inner JOIN [tbRegionTree] d ON d.regionPID=c.RegionID

)
SELECT * FROM cte
上面是找到 01 中国的。

下面为非中国的。 条件上处理下
WITH cte AS (
SELECT RegionID,RegionName,RegionPID FROM [tbRegionTree] WHERE regionPID NOT LIKE '01%'
UNION ALL
SELECT d.RegionID,d.RegionName,d.RegionPID FROM cte c inner JOIN [tbRegionTree] d ON d.regionPID=c.RegionID

)
SELECT * FROM cte

java 递归查询数据库

这个就是个树形结构,数据结构里的东西。查询后构建一个树就行了。稍后给你一个代码。

热点内容
内置存储卡可以拆吗 发布:2025-05-18 04:16:35 浏览:332
编译原理课时设置 发布:2025-05-18 04:13:28 浏览:372
linux中进入ip地址服务器 发布:2025-05-18 04:11:21 浏览:607
java用什么软件写 发布:2025-05-18 03:56:19 浏览:27
linux配置vim编译c 发布:2025-05-18 03:55:07 浏览:101
砸百鬼脚本 发布:2025-05-18 03:53:34 浏览:935
安卓手机如何拍视频和苹果一样 发布:2025-05-18 03:40:47 浏览:730
为什么安卓手机连不上苹果7热点 发布:2025-05-18 03:40:13 浏览:798
网卡访问 发布:2025-05-18 03:35:04 浏览:506
接收和发送服务器地址 发布:2025-05-18 03:33:48 浏览:368