oracle 树查询

来源:互联网 发布:计算机培训班价格java 编辑:程序博客网 时间:2024/05/29 16:11
oracle 通过STARTWITH...CONNECTBYPRIOR 语法实现树形查询,查询子节点与父节点,语法如下图:

start with:查询所用根节点,可以有多个
connect by:确定子节点与父节点的关系,如 
PRIOR c1 = c2 c1为父节点,通过c2来查找c1
c1 =PRIOR c2 c2为父节点,通过c1来查找c2
cycle:
默认为nocycle当树形查询为死循环时,会提示 "ORA-01436: CONNECT BY loop in user data"程序不执行
cycle,当树形查询为死循环时,继续执行

其他
1.CONNECT_BY_ISLEAF伪列,当节点为叶节点时为1,否则为0
2.CONNECT_BY_ROOT,查看字段的根节点,语法 CONNECT_BY_ROOT COLUMN_NAME
3.SYS_CONNECT_BY_PATH,查看当前节点路径,语法 SYS_CONNECT_BY_PATH(column_name,'/')


example

idnumber(10)notnull,--主键id
 
namevarchar2(50),--标题
 
parentnumber(10)--parent id
 
)

--父菜单
 
insertintomap(id,name,parent)values(1,'亚洲',0);
 
insertintomap(id,name,parent)values(2,'北美洲',0);
 
insertintomap(id,name,parent)values(3,'欧洲',0);
 
 
--一级菜单
 
insertintomap(id,name,parent)values(4,'中国',1);
 
insertintomap(id,name,parent)values(5,'韩国',1);
 
insertintomap(id,name,parent)values(6,'日本',1);
 
insertintomap(id,name,parent)values(7,'加拿大',2);
 
insertintomap(id,name,parent)values(8,'美国',2);
 
insertintomap(id,name,parent)values(9,'墨西哥',2);
 
insertintomap(id,name,parent)values(10,'英国',3);
 
insertintomap(id,name,parent)values(11,'法国',3);
 
insertintomap(id,name,parent)values(12,'意大利',3);

--二级菜单

insertintomap(id,name,parent)values(13,'广州',4);
 
insertintomap(id,name,parent)values(14,'上海',4);
 
insertintomap(id,name,parent)values(15,'北京',4);
 
insertintomap(id,name,parent)values(16,'首尔',5);
 
insertintomap(id,name,parent)values(17,' 蔚山',5);
 
insertintomap(id,name,parent)values(18,'釜山',5);
 
insertintomap(id,name,parent)values(19,'东京',6);
 
insertintomap(id,name,parent)values(20,'大阪',6);
 
 
insertintomap(id,name,parent)values(21,'名古屋',6);
 
insertintomap(id,name,parent)values(22,'渥太华',7);
 
insertintomap(id,name,parent)values(23,'多伦多',7);
 
insertintomap(id,name,parent)values(24,'温哥华',7);
 
insertintomap(id,name,parent)values(25,'华盛顿',8);
 
insertintomap(id,name,parent)values(26,'纽约',8);
 
insertintomap(id,name,parent)values(27,'旧金山',8);
 
insertintomap(id,name,parent)values(28,'墨西哥城',9);
 
insertintomap(id,name,parent)values(29,'奇瓦瓦市',9);
 
insertintomap(id,name,parent)values(30,'曼萨尼约港',9);
 
insertintomap(id,name,parent)values(31,'伦敦',10);
 
insertintomap(id,name,parent)values(32,'曼彻斯特',10);
 
insertintomap(id,name,parent)values(33,' 谢菲尔德',10);
 
insertintomap(id,name,parent)values(34,'巴黎',11);
 
insertintomap(id,name,parent)values(35,'马赛',11);
 
insertintomap(id,name,parent)values(36,'里昂',11);
 
insertintomap(id,name,parent)values(31,'佛罗伦萨',12);
 
insertintomap(id,name,parent)values(32,'米兰',12);
 
insertintomap(id,name,parent)values(33,' 罗马',12);
commit;


select id ,name, parent,
CONNECT_BY_ISLEAF isleaf --是否是子节点
from map m
connect by nocycle prior m.parent=m.id --parent为父节点,nocycle死循环时不执行
start with m.id= 15 or m.id=33;

    ID NAME       PARENT     ISLEAF
------ ---------- ------ ----------
    15 北京            4          0
     4 中国            1          0
     1 亚洲            0          1
    33  谢菲尔德      10          0
    10 英国            3          0
     3 欧洲            0          1

select id ,name, parent,
CONNECT_BY_ISLEAF isleaf,
CONNECT_BY_ROOT parent root, --parent的根目录
SYS_CONNECT_BY_PATH(parent, '/') path --parent的路径
from map m 
connect by  m.parent= prior m.id
start with m.id=1;

    ID NAME       PARENT     ISLEAF       ROOT PATH
------ ---------- ------ ---------- ---------- ---------------
     1 亚洲            0          0          0 /0
     4 中国            1          0          0 /0/1
    13 广州            4          1          0 /0/1/4
    14 上海            4          1          0 /0/1/4
    15 北京            4          1          0 /0/1/4
     5 韩国            1          0          0 /0/1
    16 首尔            5          1          0 /0/1/5
    17  蔚山           5          1          0 /0/1/5
    18 釜山            5          1          0 /0/1/5
     6 日本            1          0          0 /0/1
    19 东京            6          1          0 /0/1/6
    20 大阪            6          1          0 /0/1/6
    21 名古屋          6          1          0 /0/1/6

0 0
原创粉丝点击