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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - littlebamboo

【EXCEL】使用indirect()函数在excel中实现二级联动 【Excel】Excel单元格公式太长如何设置 【EXCEL】excel2007中隔行填充背景色 【EXCEL】excel中如何根据前一个下拉选项的内容.显示出下一个下拉选项内容 - littlebamboo 【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 关键字查询所在层次.

Sql代码

  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