当前位置:网站首页>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 .
边栏推荐
- 王爽汇编语言学习详细笔记一:基础知识
- DVWA exercise 05 file upload file upload
- Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
- Express
- 指针:最大值、最小值和平均值
- The number of reversing twice in leetcode simple question
- Emqtt distribution cluster and node bridge construction
- 1. Payment system
- How to transform functional testing into automated testing?
- ByteDance ten years of experience, old bird, took more than half a year to sort out the software test interview questions
猜你喜欢

The minimum number of operations to convert strings in leetcode simple problem

MySQL development - advanced query - take a good look at how it suits you

Four methods of exchanging the values of a and B

数字电路基础(二)逻辑代数
![[HCIA continuous update] advanced features of routing](/img/05/a9ed32ec8c19b236355d48f7c2ad80.jpg)
[HCIA continuous update] advanced features of routing

Sleep quality today 81 points

STC-B学习板蜂鸣器播放音乐

Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary

Keil5 MDK's formatting code tool and adding shortcuts

王爽汇编语言学习详细笔记一:基础知识
随机推荐
Numpy快速上手指南
"If life is just like the first sight" -- risc-v
Global and Chinese market of goat milk powder 2022-2028: Research Report on technology, participants, trends, market size and share
Query method of database multi table link
CSAPP家庭作业答案7 8 9章
Public key box
【指针】查找最大的字符串
Functions: Finding Roots of equations
Logstack introduction and deployment -- elasticstack (elk) work notes 019
Get started with Matplotlib drawing
Stc-b learning board buzzer plays music 2.0
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
Wang Shuang's detailed notes on assembly language learning I: basic knowledge
[pointer] use the insertion sorting method to arrange n numbers from small to large
Wang Shuang's detailed learning notes of assembly language II: registers
Quaternion -- basic concepts (Reprint)
[HCIA continuous update] working principle of static route and default route
Pointeurs: maximum, minimum et moyenne
函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
{1,2,3,2,5} duplicate checking problem