当前位置:网站首页>In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
2022-07-06 15:07:00 【Blue Star Army】
One 、 The source of the problem :
Previously in a database group , A question was raised : Now there's a watch user,user There are fields in the table username,leader, Now find out ,leader yes boss,boss Of all subordinate employees username. however boss My position is very high , His subordinates may also have subordinates , Subordinates and subordinates , There will even be many subordinates .
Two 、 The train of thought of the false great God :
at that time , When I saw someone answer like this, I immediately vomited blood , Really treat programmers as coders , Can only move bricks !!!
-- Results without vision :
select username from user
where leader = 'boss'
union
select username from user
where leader in(select username from user
where leader = 'boss';
If there is 10 Subordinate employees , Do you have to write 10 individual union all Do you ? Subqueries are nested on 9 Tier? ? Once the amount of data is large , The database must not be down .
The leader immediately let the bag go . It's a small query of subordinate employees , The database crashed .
3、 ... and 、 Use Oracle Medium start with connect by prior
start with connect by prior Is a recursive query usage .
We'll take Oracle Employee table in the self-contained database emp For example :
-- A simple line of code :
select * from emp start with empno = 7839 connect by prior empno = mgr;
give the result as follows :
This structure is emp Employee surface level chart : Four 、 Tell me about start with connect by prior Usage of :
1、connect by The grammar of :
select ... from tablename
start by cond1
connect by prior cond2
where cond3
2、 Introduction of each part :
start with Clause is optional , It is used to identify which node is the root node of the tree structure . If the clause is omitted , It means that all rows that meet the query criteria are taken as root nodes .
cond1 Is the root node qualifier , Of course, you can relax the restrictions , To get multiple root nodes , It's actually multiple trees .
prior The operator must be placed before one of the two columns of the join relationship . For parent-child relationships between nodes ,prior Operator represents the parent node on one side , On the other side is the child node , To determine whether the search tree structure is top-down or bottom-up . In a connected relationship , In addition to using column names , List expressions are also allowed .
cond2 It's the connection condition , Among them prior Indicates the previous record , such as connect by prior id=praentid That is, the last record id It's from this record praentid, That is, the father of this record is the last record .
cond3 It's filter conditions , Used to filter all returned records .
5、 ... and 、 Final use :
start with connect by prior Recursive query usage : This clause is mainly used for B Data recursive query of tree structure type , give B Any node in the tree structure type , Traverse its final parent node or child node .
边栏推荐
- Réponses aux devoirs du csapp 7 8 9
- [pointer] find the length of the string
- Function: string storage in reverse order
- Global and Chinese market for antiviral coatings 2022-2028: Research Report on technology, participants, trends, market size and share
- JDBC 的四种连接方式 直接上代码
- Pointeurs: maximum, minimum et moyenne
- 后台登录系统,JDBC连接数据库,做小案例练习
- Function: find the root of the equation by Newton iterative method
- 【指针】删除字符串s中的所有空格
- Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
猜你喜欢
数字电路基础(三)编码器和译码器
ucore lab6 调度器 实验报告
Wang Shuang's detailed learning notes of assembly language II: registers
王爽汇编语言详细学习笔记二:寄存器
Stc-b learning board buzzer plays music
Logstack introduction and deployment -- elasticstack (elk) work notes 019
Maximum nesting depth of parentheses in leetcode simple questions
5分钟掌握机器学习鸢尾花逻辑回归分类
Summary of thread implementation
“Hello IC World”
随机推荐
【指针】求二维数组中最大元素的值
Fundamentals of digital circuit (V) arithmetic operation circuit
Thinking about three cups of tea
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
【指针】求解最后留下的人
Numpy快速上手指南
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share
"If life is just like the first sight" -- risc-v
Database monitoring SQL execution
Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Using flask_ Whooshalchemyplus Jieba realizes global search of flask
ucore lab2 物理内存管理 实验报告
Common Oracle commands
CSAPP家庭作業答案7 8 9章
Four methods of exchanging the values of a and B
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
C language do while loop classic Level 2 questions
[pointer] find the length of the string