惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
WordPress大学
WordPress大学
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
L
LangChain Blog
雷峰网
雷峰网
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
NISL@THU
NISL@THU
Scott Helme
Scott Helme
量子位
S
Security Affairs
T
Threat Research - Cisco Blogs
博客园_首页
云风的 BLOG
云风的 BLOG
D
Docker
AWS News Blog
AWS News Blog
腾讯CDC
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
U
Unit 42
Recent Announcements
Recent Announcements
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
T
The Exploit Database - CXSecurity.com
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
L
Lohrmann on Cybersecurity
The Hacker News
The Hacker News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Y
Y Combinator Blog
S
Security @ Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Check Point Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
I
InfoQ

博客园 - littlebamboo

【EXCEL】使用indirect()函数在excel中实现二级联动 【Excel】Excel单元格公式太长如何设置 【EXCEL】excel2007中隔行填充背景色 【EXCEL】excel中如何根据前一个下拉选项的内容.显示出下一个下拉选项内容 【office】在excel表中能引用另一个sheet表中的数据,以下拉菜单形式筛选 【mantis】(转载)Apache 2.2.11 + PHP 5.2.9 + MySQL 5.1.34 + Mantis 1.1.7配置指南 信息系统项目管理师初了解 EXCEL2007怎样设置某单元格的值为 OK 的时候 整行设置为绿色 电子签章 解决word中就出现了无法使用鼠标拖动和选取页面文字的问题 【转】11个相似图片搜索网站 【PPT】在PPT中插入时间和页码 如何用excel打开后缀为txt的记事本文档,将记事本里用逗号分隔的文本转换成excel表格 【.NET】.net学习疑问汇总 【.NET】WebForm与Asp.NET MVC区别学习 【.NET】Razor学习 【SmartDraw】SmartDraw绘图种类初步认识 【.NET】开始学习.NET MVC框架的搭建 SQLite初识
【转】oracle中的递归查询
littlebamboo · 2011-02-17 · via 博客园 - littlebamboo

遇到的需求是查看我们给用户建立的站点的栏目被调整了N多次后现在是什么样子了,于是在网上找到相关的资料,用oracle特有的语句查询。

详细内容转自http://www.javaeye.com/topic/287749

Start with...Connect By子句递归查询一般用于一个表维护树形结构的应用。

创建示例表:

CREATE TABLE TBL_TEST
(
  ID    NUMBER,
  NAME  VARCHAR2(100 BYTE),
  PID   NUMBER                                  DEFAULT 0
);

插入测试数据:

INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('1','10','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('2','11','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('3','20','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('4','12','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('5','121','2');

从Root往树末梢递归

select * from TBL_TEST
 start with id=1
 connect by prior id = pid

从末梢往树ROOT递归

select * from TBL_TEST
 start with id=5
 connect by prior pid = id

=====

对于oracle进行简单树查询(递归查询)

DEPTID PAREDEPTID NAME
NUMBER NUMBER CHAR (40 Byte)
部门id 父部门id(所属部门id) 部门名称

通过子节点向根节点追朔.

Sql代码 复制代码

  1. select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid   

Sql代码 复制代码

  1. select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid   

通过根节点遍历子节点.

Sql代码 复制代码

  1. select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid   

Sql代码 复制代码

  1. select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid   

可通过level 关键字查询所在层次.

  1. select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid   

Sql代码 复制代码

  1. select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid   

再次复习一下:start with ...connect by 的用法, start with 后面所跟的就是就是递归的种子

递归的种子也就是递归开始的地方 connect by 后面的"prior" 如果缺省:则只能查询到符合条件的起始行,并不进行递归查询;

connect by prior 后面所放的字段是有关系的,它指明了查询的方向

其他参考:

http://zhaolinjnu.blog.163.com/blog/static/2280005200841621159121/

http://www.javaeye.com/topic/287749

http://www.javaeye.com/wiki/topic/746687