递归查询数据库
① 数据库语句的递归查询求助
应该是这样:
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 递归查询数据库
这个就是个树形结构,数据结构里的东西。查询后构建一个树就行了。稍后给你一个代码。