oralce递归查询

1.基本语法是:
select ... from <TableName>
where <Conditional-1>
start with <Conditional-2>
connect by <Conditional-3>;
//<Conditional-1>:过滤条件,用于对返回的所有记录进行过滤。
//<Conditional-2>:查询结果重起始根结点的限定条件。
//<Conditional-3>:连接条件
//如果connect by prior中的prior被省略,则查询将不进行深层递归。
2.向上查询当前菜单及所有上级菜单
select t.* from s_menu t start with t.id='510' connect by prior t.fid = t.id
3.向上查询所有上级菜单(不包含当前菜单)
select t.* from s_menu t start with t.id='510' connect by prior t.fid = t.id
4.向下查询当前菜单及所有下级菜单
select t.* from s_menu t start with t.id='001' connect by prior t.id = t.fid 
5.向下查询所有下级菜单(不包含当前菜单)
select t.* from s_menu t start with t.id='001' connect by prior t.id = t.fid
6.查询递归路径
select t.id,t.name,fid,substr(sys_connect_by_path(NAME,'->'),3) menu_path
from s_menu t start with t.name = '系统功能' connect by prior t.id = t.fid order by t.id
7.分层次显示
select t.id,lpad('|-',(level-1)*4,'|-')||lpad('『',2)||t.name||rpad('』',2) as newname
from s_menu t connect by prior t.id=t.fid start with t.id='-1'
总结
向上递归和向下递归的关键就是最后一个条件,父ID等于ID则向上递归,反之向下递归。

参考:http://www.cnblogs.com/wanghonghu/archive/2012/08/31/2665945.html

相关文章

这篇文章主要介绍“hive和mysql的区别是什么”,在日常操作中...
这篇“MySQL数据库如何改名”文章的知识点大部分人都不太理解...
这篇文章主要介绍“mysql版本查询命令是什么”的相关知识,小...
本篇内容介绍了“mysql怎么修改字段的内容”的有关知识,在实...
这篇文章主要讲解了“mysql怎么删除unique约束”,文中的讲解...
今天小编给大家分享一下mysql怎么查询不为空的字段的相关知识...