zoukankan      html  css  js  c++  java
  • oracle 树形查询,自连接查询

    例1

    select employee_id, first_name,last_name,manager_id
    from employees  
    start with employee_id=100
    connect by prior employee_id=manager_id
    1    Steven King
    2    -Neena Kochhar
    3    --Nancy Greenberg
    4    ---Daniel Faviet
    4    ---John Chen
    ...

    例2

    create table my_system_menu
    ( menu_id number,
      title varchar2(100),
      parent_id number,
      constraint my_system_menu_pk primary key (menu_id ),
      constraint my_system_menu_fk1 FOREIGN key( parent_id) references my_system_menu(menu_id) 
      );
      
    insert into my_system_menu  values (1,'Banner',null);
    insert into my_system_menu  values (2,'Student',1);
    insert into my_system_menu  values (3,'Course Catalog',2);
    insert into my_system_menu  values (4,'Class Schedule',2);
    insert into my_system_menu  values (5,'General Person',2);
    insert into my_system_menu  values (6,'Faculty Load',2);
    insert into my_system_menu  values (7,'Location Management ',2);
    insert into my_system_menu  values (8,'Basic Course Information',3);
    insert into my_system_menu  values (9,'Course Detail Information',3);
    insert into my_system_menu  values (10,'Course Registration Restrictions',3);
    commit;
    select  level,
            case 
            when level=1 then title 
            when level>1 then lpad (title,length(title)+level-1, '-')
            end tree
    from my_system_menu  
    start with menu_id=1
    connect by prior menu_id=parent_id 
    order by menu_id

    每天进步一点点,多思考,多总结 版权声明:本文为CNblog博主「zaituzhong」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
  • 相关阅读:
    响应式笔记(1)
    javascript复制文章加版权声明代码
    div的水平和垂直居中
    javascript随机打乱数组
    javascript操作字符串的方法
    《Javascript高级程序设计》读书笔记(1-3章)
    一个将 footer 保持在底部的最好方法
    Python内置的字符串处理函数整理
    c c++怎么判断一个字符串中是否含有汉字
    shell的if判断
  • 原文地址:https://www.cnblogs.com/tingxin/p/14737394.html
Copyright © 2011-2022 走看看