当前位置:网站首页>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 .
边栏推荐
- Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
- C language do while loop classic Level 2 questions
- Fundamentals of digital circuit (V) arithmetic operation circuit
- [pointer] use the insertion sorting method to arrange n numbers from small to large
- Cc36 different subsequences
- Stc-b learning board buzzer plays music 2.0
- Get started with Matplotlib drawing
- Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- HackTheBox-Emdee five for life
- The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
猜你喜欢

Réponses aux devoirs du csapp 7 8 9

Daily code 300 lines learning notes day 9

Rearrange spaces between words in leetcode simple questions

UCORE lab7 synchronous mutual exclusion experiment report

Install and run tensorflow object detection API video object recognition system of Google open source

1. Payment system

Description of Vos storage space, bandwidth occupation and PPS requirements

Keil5-MDK的格式化代码工具及添加快捷方式

Sleep quality today 81 points

High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
随机推荐
“Hello IC World”
Fundamentals of digital circuit (V) arithmetic operation circuit
Investment should be calm
Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
指针:最大值、最小值和平均值
Software testing interview summary - common interview questions
Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
Pointeurs: maximum, minimum et moyenne
Wang Shuang's detailed notes on assembly language learning I: basic knowledge
The minimum sum of the last four digits of the split digit of leetcode simple problem
ucore lab6 调度器 实验报告
Keil5-MDK的格式化代码工具及添加快捷方式
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
MySQL development - advanced query - take a good look at how it suits you
函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
指针--剔除字符串中的所有数字
If the position is absolute, touchablehighlight cannot be clicked - touchablehighlight not clickable if position absolute
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
Face and eye recognition based on OpenCV's own model
Detailed introduction to dynamic programming (with examples)