当前位置:网站首页>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 .
边栏推荐
- [Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
- Es full text index
- Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
- What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
- {1,2,3,2,5} duplicate checking problem
- STC-B学习板蜂鸣器播放音乐2.0
- Global and Chinese markets of cobalt 2022-2028: Research Report on technology, participants, trends, market size and share
- Numpy快速上手指南
- “Hello IC World”
- 指针:最大值、最小值和平均值
猜你喜欢

Rearrange spaces between words in leetcode simple questions

ucore lab7 同步互斥 实验报告

Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers

Réponses aux devoirs du csapp 7 8 9

Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025

The minimum sum of the last four digits of the split digit of leetcode simple problem

The number of reversing twice in leetcode simple question
![[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases](/img/31/875b08d752ecd914f4e727e561adbd.jpg)
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases

Stc-b learning board buzzer plays music

1. Payment system
随机推荐
Install and run tensorflow object detection API video object recognition system of Google open source
The four connection methods of JDBC are directly coded
[pointer] find the largest string
Opencv recognition of face in image
指针 --按字符串相反次序输出其中的所有字符
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
Function: string storage in reverse order
Report on the double computer experiment of scoring system based on 485 bus
Description of Vos storage space, bandwidth occupation and PPS requirements
Public key box
Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
C language learning summary (I) (under update)
[pointer] delete all spaces in the string s
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
Capitalize the title of leetcode simple question
Want to learn how to get started and learn software testing? I'll give you a good chat today
Investment should be calm
【指针】查找最大的字符串
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008