zoukankan      html  css  js  c++  java
  • 【SQL查询】树结构查询

    格式:
    SELECT ... FROM + 表名 WHERE + 条件3 START WITH + 条件1 CONNECT BY PRIOR + 条件2

    条件1: 表示从哪个节点开始查找, 也就是通过条件1 查询到的数据, 作为后续查询的起始节点(参数).
    条件2: 是连接条件,其中用PRIOR表示上一条记录,例如CONNECT BY PRIOR ID = PID,意思就是上一条记录的ID是本条记录的PID,即本记录的父亲是上一条记录。
    条件3: 不能用在 Connect By 后, 这里的条件判断, 等价于 在最后查询出结果列表之后, 再进行条件筛选; 并非 删除掉 节点及子节点;

    关键点:
    nocycle关键字, 有时候数据本身 不合理会导致出现循环的问题, 如 将上述的 ID '00001' 记录的 'PID' 也改为 '00001', 会出现循环的问题, 这是, 需要用到 nocycle 即可消除循环;
    Connect By nocycle Prior ID = PID 即可.
    connect_by_isleaf 表示当前节点是否是叶子节点
    level 表示当前节点所处层级, 这里的层级指的是 从 start with 查询到的节点开始往下算起, 当前属于第几层级

    示例:
    (1) 脚本
    /* 表格 */
    create table tb_menu(
    	id number(10) not null,
    	title varchar(50),
    	parent number(10)
    )
    --父菜单
    insert into tb_menu(id, title, parent) values(1, '父菜单1',null);
    insert into tb_menu(id, title, parent) values(2, '父菜单2',null);
    insert into tb_menu(id, title, parent) values(3, '父菜单3',null);
    insert into tb_menu(id, title, parent) values(4, '父菜单4',null);
    insert into tb_menu(id, title, parent) values(5, '父菜单5',null);
    --一级菜单
    insert into tb_menu(id, title, parent) values(6, '一级菜单6',1);
    insert into tb_menu(id, title, parent) values(7, '一级菜单7',1);
    insert into tb_menu(id, title, parent) values(8, '一级菜单8',1);
    insert into tb_menu(id, title, parent) values(9, '一级菜单9',2);
    insert into tb_menu(id, title, parent) values(10, '一级菜单10',2);
    insert into tb_menu(id, title, parent) values(11, '一级菜单11',2);
    insert into tb_menu(id, title, parent) values(12, '一级菜单12',3);
    insert into tb_menu(id, title, parent) values(13, '一级菜单13',3);
    insert into tb_menu(id, title, parent) values(14, '一级菜单14',3);
    insert into tb_menu(id, title, parent) values(15, '一级菜单15',4);
    insert into tb_menu(id, title, parent) values(16, '一级菜单16',4);
    insert into tb_menu(id, title, parent) values(17, '一级菜单17',4);
    insert into tb_menu(id, title, parent) values(18, '一级菜单18',5);
    insert into tb_menu(id, title, parent) values(19, '一级菜单19',5);
    insert into tb_menu(id, title, parent) values(20, '一级菜单20',5);
    --二级菜单
    insert into tb_menu(id, title, parent) values(21, '二级菜单21',6);
    insert into tb_menu(id, title, parent) values(22, '二级菜单22',6);
    insert into tb_menu(id, title, parent) values(23, '二级菜单23',7);
    insert into tb_menu(id, title, parent) values(24, '二级菜单24',7);
    insert into tb_menu(id, title, parent) values(25, '二级菜单25',8);
    insert into tb_menu(id, title, parent) values(26, '二级菜单26',9);
    insert into tb_menu(id, title, parent) values(27, '二级菜单27',10);
    insert into tb_menu(id, title, parent) values(28, '二级菜单28',11);
    insert into tb_menu(id, title, parent) values(29, '二级菜单29',12);
    insert into tb_menu(id, title, parent) values(30, '二级菜单30',13);
    insert into tb_menu(id, title, parent) values(31, '二级菜单31',14);
    insert into tb_menu(id, title, parent) values(32, '二级菜单32',15);
    insert into tb_menu(id, title, parent) values(33, '二级菜单33',16);
    insert into tb_menu(id, title, parent) values(34, '二级菜单34',17);
    insert into tb_menu(id, title, parent) values(35, '二级菜单35',18);
    insert into tb_menu(id, title, parent) values(36, '二级菜单36',19);
    insert into tb_menu(id, title, parent) values(37, '二级菜单37',20);
    --三级菜单
    insert into tb_menu(id, title, parent) values(38, '三级菜单38',21);
    insert into tb_menu(id, title, parent) values(39, '三级菜单39',22);
    insert into tb_menu(id, title, parent) values(40, '三级菜单40',23);
    insert into tb_menu(id, title, parent) values(41, '三级菜单41',24);
    insert into tb_menu(id, title, parent) values(42, '三级菜单42',25);
    insert into tb_menu(id, title, parent) values(43, '三级菜单43',26);
    insert into tb_menu(id, title, parent) values(44, '三级菜单44',27);
    insert into tb_menu(id, title, parent) values(45, '三级菜单45',28);
    insert into tb_menu(id, title, parent) values(46, '三级菜单46',28);
    insert into tb_menu(id, title, parent) values(47, '三级菜单47',29);
    insert into tb_menu(id, title, parent) values(48, '三级菜单48',30);
    insert into tb_menu(id, title, parent) values(49, '三级菜单49',31);
    insert into tb_menu(id, title, parent) values(50, '三级菜单50',31);
    commit;
    

    (2) select level, t.* from tb_menu t start with id = 1 connect by prior t.id = t.parent;

    (3)select level, t.id, lpad(' ', level * 4) || t.title as title, t.parent
      from tb_menu t
     start with id = 6
    connect by prior t.id = t.parent;

    id=6为起始节点,上一条记录的id是本记录的parent。也称为:本记录的parent是上一条记录的id

    (4)select level, t.* from tb_menu t start with id = 6 connect by t.id = prior t.parent;

    id=6为起始节点,上一条记录的parent是本记录的id。也称为:本记录的id是上一条记录的parent

  • 相关阅读:
    win7下安装Linux实现双系统全攻略
    Dreamweaver_CS6安装与破解,手把手教程
    windows Server 2008各版本有何区别?
    如何查看路由器中的pppoe拨号密码?
    xp远程桌面连接最大用户数怎么设置?
    网站的盈利模式
    linux 下安装mysql-5.7.16
    GNS3连接虚拟机
    cain使用教程
    数据中心网络架构的问题与演进 — CLOS 网络与 Fat-Tree、Spine-Leaf 架构
  • 原文地址:https://www.cnblogs.com/zhuhaiying/p/9664290.html
Copyright © 2011-2022 走看看