树结构递归查询
来源:互联网 发布:手机淘宝货品上架教程 编辑:程序博客网 时间:2024/05/16 06:19
表recursion数据如下:
id name parentid
1 食品分类 -1
2 肉类 1
3 蔬菜类 1
4 产品分类 -1
5 保健品 4
6 医药 4
7 建筑 4
一 ORACLE中实现方法:
Oracle中直接支持,使用语句select * from tablename start with connect by
prior id(子层的列)=parentid(属于顶层的列)
语句说明:
start with 指定层次开始的条件,即满足这个条件的行即可以作为层次树的最顶层
connect by prior指层之间的关联条件,即什么样的行是上层行的子行(自连接条件)
实例:
select * from recursionstart with connect by prior >查询结果:
id name parentid
1 食品分类 -1
2 肉类 1
3 蔬菜类 1
二 MSSQL中的实现方法
在MSSQL中需要使用临时表和循环多次查询的方式实现.
创建函数:
create function GetRecursion(@id int)
returns @t table(
id int,
name varchar(50),
parentid int
)
as
begin
insert @t select * from recursion where > while @@rowcount>0
insert @t select a.* from recursion as a inner join @t as b
on a.parentid=b.id and a.id not in(select id from @t)
return
end
使用方法:
select * from GetRecursion(4)
查询结果:
id name parentid
4 产品分类 -1
5 保健品 4
6 医药 4
7 建筑 4
三 MYSQL中的实现方法
查询语句:
select b.id,b.name,b.parentid from recursion as a, recursion as b where
a.id=b.parentid and (a.id=1 or a. parentid =1)
查询结果:
id name parentid
2 肉类 1
3 蔬菜类 1
四 在ORACLE、MSSQL、MYSQL中可以使用下面的查询语句只返回树结构表的子结点数据
select *
from tablename t
where not exists (select 'X'
from tablename t1, tablename t2
where t1.id = t2.parentid
and t1.id = t.id)
如:
select *
from recursion t
where not exists (select 'X'
from recursion t1, recursion t2
where t1.id = t2.parentid
and t1.id = t.id)
查询结果:
id name parentid
2 肉类 1
3 蔬菜类 1
5 保健品 4
6 医药 4
7 建筑 4
五 在ORACLE、MSSQL、MYSQL中可以使用下面的查询语句只返回树结构表的根结点数据
select *
from tablename t
where not exists (select 'X'
from tablename t1, tablename t2
where t1.id = t2.parentid
and t1.id = t. parentid)
如:
select *
from recursion t
where not exists (select 'X'
from recursion t1, recursion t2
where t1.id = t2.parentid
and t1.id = t. parentid)
查询结果:
id name parentid
1 食品分类 -1
4 产品分类 -1
id name parentid
1 食品分类 -1
2 肉类 1
3 蔬菜类 1
4 产品分类 -1
5 保健品 4
6 医药 4
7 建筑 4
一 ORACLE中实现方法:
Oracle中直接支持,使用语句select * from tablename start with connect by
prior id(子层的列)=parentid(属于顶层的列)
语句说明:
start with 指定层次开始的条件,即满足这个条件的行即可以作为层次树的最顶层
connect by prior指层之间的关联条件,即什么样的行是上层行的子行(自连接条件)
实例:
select * from recursionstart with connect by prior >查询结果:
id name parentid
1 食品分类 -1
2 肉类 1
3 蔬菜类 1
二 MSSQL中的实现方法
在MSSQL中需要使用临时表和循环多次查询的方式实现.
创建函数:
create function GetRecursion(@id int)
returns @t table(
id int,
name varchar(50),
parentid int
)
as
begin
insert @t select * from recursion where > while @@rowcount>0
insert @t select a.* from recursion as a inner join @t as b
on a.parentid=b.id and a.id not in(select id from @t)
return
end
使用方法:
select * from GetRecursion(4)
查询结果:
id name parentid
4 产品分类 -1
5 保健品 4
6 医药 4
7 建筑 4
三 MYSQL中的实现方法
查询语句:
select b.id,b.name,b.parentid from recursion as a, recursion as b where
a.id=b.parentid and (a.id=1 or a. parentid =1)
查询结果:
id name parentid
2 肉类 1
3 蔬菜类 1
四 在ORACLE、MSSQL、MYSQL中可以使用下面的查询语句只返回树结构表的子结点数据
select *
from tablename t
where not exists (select 'X'
from tablename t1, tablename t2
where t1.id = t2.parentid
and t1.id = t.id)
如:
select *
from recursion t
where not exists (select 'X'
from recursion t1, recursion t2
where t1.id = t2.parentid
and t1.id = t.id)
查询结果:
id name parentid
2 肉类 1
3 蔬菜类 1
5 保健品 4
6 医药 4
7 建筑 4
五 在ORACLE、MSSQL、MYSQL中可以使用下面的查询语句只返回树结构表的根结点数据
select *
from tablename t
where not exists (select 'X'
from tablename t1, tablename t2
where t1.id = t2.parentid
and t1.id = t. parentid)
如:
select *
from recursion t
where not exists (select 'X'
from recursion t1, recursion t2
where t1.id = t2.parentid
and t1.id = t. parentid)
查询结果:
id name parentid
1 食品分类 -1
4 产品分类 -1
- 树结构递归查询
- sql 树型结构查询 递归方式
- Oracle 循环递归遍历树结构查询
- 递归查询表结构
- java 递归查询树形结构
- java tree 结构递归查询
- java递归与反向递归(逆向递归)查询树tree结构根据关键字过滤数据
- 关于树型结构数据递归查询,转非递归查询的实现
- PostgreSQL递归查询实现树状结构查询
- Oracle递归查询(目录结构查询)
- 递归查询(树查询)
- 树形结构表记录 :sql递归查询
- oracle 递归 树形结构数据查询
- oracle 递归查询 树查询
- oracle 递归查询 树查询
- Oracle 递归查询 树查询
- oracle树查询(递归查询)
- java 递归树结构
- 按钮在 IE 中两边被拉伸的BUG
- Spring事务属性
- 斐波拉挈 递归实现
- 热商掉的>
- 臀部大的美女最令男人着迷
- 树结构递归查询
- 五大互联网巨头60天来市值蒸发1200亿美元
- SQL_ExecuteNonQuery()
- Struts2 在标签的ID属性中 获取 索引
- HTTP长连接服务器端推技术
- IR with Lemur,Step by step
- 掌握三条简单net命令保证网络安全检测
- 用awk下载优酷视频
- 巧用系统命令看是否中毒