【SQL 学习】层次化查询之CONNECT BY 和 START WITH

简介: SQL> select employee_id ,manager_id ,first_name ,last_name  2  from emp  3  start with employee_id =1--- 定义层次化查询的起点  4  connect by prior employee_id = manager_id;指定父行与子行之间的关系。


SQL> select employee_id ,manager_id ,first_name ,last_name
  2  from emp
  3  start with employee_id =1--- 定义层次化查询的起点
  4  connect by prior employee_id = manager_id;指定父行与子行之间的关系。也就是父节点的employee_id 等于  子节点的manager_id

EMPLOYEE_ID MANAGER_ID FIRST_NAME LAST_NAME
-----------               ----------           ---------------       ----------------
          1                                              James      Smith
          2                         1                   Ron           Johnson
          3                         2                   Fred          Hobbs
          5                         2                   Rob           Green
          4                         1                   Susan      Jones
          6                         4                   Jane         Brown
          9                         6                   Henry        Heyson
          7                         4                   John         Grey                   
          8                         7                   Jean         Blue                      
         10                       1                    Kevin        Black              
         11                     10                    Keith         Long
         12                     10                    Frank        Howard
         13                     10                    Doreen     Penn

已选择13行。
执行计划
----------------------------------------------------------                                                                       
Plan hash value: 1213993571               
                                                                                                                 
------------------------------------------------------------------------------------------------
| Id  | Operation                               | Name | Rows  | Bytes | Cost (%CPU)| Time     |               
------------------------------------------------------------------------------------------------               
|   0 | SELECT STATEMENT                        |      |    13 |   234 |     3   (0)| 00:00:01 |              
|*  1 |  CONNECT BY NO FILTERING WITH START-WITH|      |       |       |            |          |              
|   2 |   TABLE ACCESS FULL                     | EMP  |    13 |   234 |     3   (0)| 00:00:01 |              
------------------------------------------------------------------------------------------------               
Predicate Information (identified by operation id):
---------------------------------------------------               
                                                                                                                                 
   1 - access("MANAGER_ID"=PRIOR "EMPLOYEE_ID")               
       filter("EMPLOYEE_ID"=1)

目录
相关文章
|
SQL
面试1 SQL SERVER 查询第20行到30之间的数据
SQL SERVER 查询第20行到30之间的数据 1.先查询前20行的ID,后查询除去20条记录的前10条记录 SELECT TOP 10 * FROM tbBank WHERE BankID NOT IN(SELECT TOP 20 BankID FROM tbBank ORDER BY BankID ASC) 2.
1965 0
|
SQL 索引 数据库
sql server 索引阐述系列八 统计信息
原文:sql server 索引阐述系列八 统计信息 一.概述     sql server在快速查询值时只有索引还不够,还需要知道操作要处理的数据量有多少,从而估算出复杂度,选择一个代价小的执行计划,这样sql server就知道了数据的分布情况。
1007 0
|
SQL 机器学习/深度学习 Go
SQL Server如何用SQL实现一批字符串的全部组合
原文:SQL Server如何用SQL实现一批字符串的全部组合 在SQL Server中,如何用SQL去实现得到一批字符串的全部组合呢?这个是同事在实际需求当中遇到的一个问题,他的具体需求如下所示:   传入参数格式为'1,2,3,224,15,6'   'A,BC,GHT,TTY,B,E' ...
1069 0