Start with...Connect By

news/2024/7/7 22:18:09
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进行简单树查询(递归查询)

 

DEPTIDPAREDEPTIDNAME
NUMBERNUMBERCHAR (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 后面所放的字段是有关系的,它指明了查询的方向

练习: 通过子节点获得顶节点

Sql代码  复制代码
  1. select FIRST_VALUE(deptid) OVER (ORDER BY LEVEL DESC ROWS UNBOUNDED PRECEDING) AS firstdeptid from persons.dept start with deptid=76 connect by prior paredeptid=deptid  

====这种方法只是当表里就有一颗树,多棵树怎么办?

转载于:https://www.cnblogs.com/jianming-chan/p/3283114.html


http://www.niftyadmin.cn/n/3051396.html

相关文章

linux下使用正确的用户名密码,本地无法连接mysql

问题现象: Linux系统为CentOS 7.0 64位,通过IP远程mysql时,可以正常访问,确定账号密码没有问题。但是本地连接mysql时,提示ERROR 1045 (28000): Access denied for user RnE0LXGMQhHNlocalhost (using password: YES)&…

IPV6基本知识

IPV6基本知识基本概念基本概念 IPv6是由八组,每组四位16进制数字组成,每组之间由":"来分隔。 简单的例子: 2610:00f8:0c34:67f9:0200:83ff:fe94:4c36,每个“:”前后都是4位16进制的数字&#x…

7.3 多元回归分析(multiple regression)

1. 与简单线性回归区别(simple linear regression) 多个自变量(x) 2. 多元回归模型 yβ0+β1x1β2x2 ... βpxpε 其中:β0,β1,β2... βp是参数 ε是误差值 3. 多元回归方程 E(y)β0+β&#…

UVA 10905 Children's Game 孩子的游戏 贪心

题意:给出N个数,要求把它们拼凑起来,让得到的数值是最大的。 只要分别比较两个数放前与放后的值的大小,排序后输出就可以了。 比如123和56,就比较12356和56123的大小就行了。 写一个比较函数,然后用sort调用…

使用mongoose操作mongodb数据库

1、如何启动mongodb数据库 参考地址:http://www.runoob.com/mongodb/mongodb-window-install.html 在数据库安装的地方,bin文件夹,输入 mongod --dbpath d:\data\db d:\data\db 是保存数据的文件夹 2、代码 1 var mongooserequire(mongoose);…

git初始化仓库并上传到服务器

git初始化仓库并上传到服务器简易的命令行入门教程Git 全局设置:创建 reptile 项目git 仓库:已有仓库?简易的命令行入门教程 Git 全局设置: git config --global user.name "xxxx" git config --global user.email "xxxxqq.com"创建 reptile 项目git 仓…

mybatis返回int类型报null

解决这个问题,是当查出来为NULL时,结一个默认值,如:0。 MySQL: SELECT IFNULL(MAX(id),0)AS sort FROM table Oracle: SELECT nvl(MAX(id),0) as sort FROM table转载于:https://www.cnblogs.com/start-fxw/p/6026510.…

7.4 多元回归分析(multiple regression)应用

1. 例子 一家快递公司送货:X1: 运输里程 X2: 运输次数 Y:总运输时间 Driving Assignment X1Miles Traveled X2Number of Deliveries Y Travel Time (Hours) 1 100 4 9.3 2 50 3 4.8 3 100 4 8.9 4 100…