Get the App
SLTechnology News&Howtos  ›  Database  › 

Tree structure and hierarchical query in oracle Table

Shulou Source: shulou.com Published: 2022-06-01 15:21:42 10月03日 Update

1. Here, take the emp table as an example. There is a subordinate-superior relationship between the empno field and the mgr field in this table.

2 look up the top boss from top to bottom, where the level is one of the three pseudo columns

Select level,empno,ename,mgr from emp

Start with empno=7839-subordinate field

Connect by prior empno=mgr;-- subordinate field = superior field

3 check from bottom to top

Select level,empno,ename,mgr from emp

Start with empno=7369-subordinate field

Connect by prior mgr=empno;-- parent field = subordinate field

4-- just filter a single node. Everything under that node is preserved.

Select level,empno,ename,mgr from emp

Where empno7698-subordinate field

Start with empno=7839

Connect by prior empno=mgr;-- subordinate field = superior field

5-filter the entire branch

Select level,empno,ename,mgr from emp

Start with empno=7839-subordinate field

Connect by prior empno=mgr and empno7698;-- subordinate field = superior field

Tags: Fields subordinates superiors nodes three big branch single just level top level tree structure query Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Xiaomi MariaDB Huawei macOS