简体   繁体   English

在Oracle SQL中连接

[英]Connect by in Oracle SQL

Suppose that we have following tables 假设我们有以下表格

create table Employee(
  2    EMPNO         NUMBER(3),
  3    ENAME         VARCHAR2(15 BYTE),
  4    HIREDATE      DATE,
  5    ORIG_SALARY   NUMBER(6),
  6    CURR_SALARY   NUMBER(6),
  7    REGION        VARCHAR2(1 BYTE),
  8    MANAGER_ID    NUMBER(3)
  9  )

and

create table job (
  2    EMPNO         NUMBER(3),
  3    jobtitle      VARCHAR2(20 BYTE)
  4  )
  5  /

I am interested in what the below query does 我对以下查询的作用感兴趣

SELECT empno, manager_id, ename
  2  FROM employee
  3  START WITH empno = 1
  4  CONNECT BY PRIOR empno = manager_id;

As I understood this code selects empno , manager_id , ename from employee, it starts selection from this row where empno=1 , but could not understand what this line does: 据我empno ,这段代码选择了empnomanager_id ,来自员工的ename ,它从empno=1这一行开始选择,但无法理解这一行的作用:

CONNECT BY PRIOR empno = manager_id;

Is this the same as: 这是一样的:

where empno=manager_id?
CONNECT BY PRIOR empno = manager_id;

This will produce the recursion. 这将产生递归。 All records that are part of the next lower hierarchical level will return. 将返回属于下一个较低层级的所有记录。 This will return a hierarchy from top to bottom for all managers and their respective under working subordinates. 这将为所有经理及其各自的工作下属返回从上到下的层次结构。

30 (manager_id)
   12 
   5 (manager_id)
      1
      7
20 (manager_id)
   15
   10

The query is recursive, it start from employee #1 (CEO probably) and then recursively prints all his subordinates and then all their subordinates and so on and so forth (until all the employees are printed). 该查询是递归的,它从员工#1(可能是CEO)开始,然后递归打印他的所有下属,然后是他们所有的下属,依此类推等等(直到所有员工都被打印出来)。

A good explanation about the "START WITH" and "CONNECT BY" can be found here 关于“START WITH”和“CONNECT BY”的一个很好的解释可以在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM