通过指定根节点和叶节点进行oracle层次查询

时间:2016-04-07 20:53:37

标签: sql oracle oracle11g

我有一个带有以下字段的oracle表:MgrID,EmpID,Name。

层次结构实际上从CEO层开始,但我想指定CIO的EmpID作为特定员工的根和EmpID,并获得该员工的层次结构。如何在Oracle中构建该查询?

2 个答案:

答案 0 :(得分:1)

在orcale中,您可以使用带开头的选择和

连接
 select  * from your_table  
 start with EmpID = yuor_value
 connect by prior your_child = your_parent;

答案 1 :(得分:0)

Oracle安装程序

SELECT *
FROM   table_name
START WITH role = 'CIO'
CONNECT BY PRIOR MgrID = EmpID;

<强>查询

     EMPID      MGRID NAME       ROLE     
---------- ---------- ---------- ----------
         4          2 Deb        CIO        
         2          1 Bob        Director   
         1            Amy        CEO        

<强>输出

class RunClientCommand(Command):
    """
    A command class to runs the client GUI.
    """

    description = "runs client gui"

    # The format is (long option, short option, description).
    user_options = [
        ('socket=', None, 'The socket of the server to connect (e.g. '127.0.0.1:8000')',
    ]

    def initialize_options(self):
        """
        Sets the default value for the server socket.

        The method is responsible for setting default values for
        all the options that the command supports.

        Option dependencies should not be set here.
        """
        self.socket = '127.0.0.1:8000'

    def finalize_options(self):
        """
        Overriding a required abstract method.

        The method is responsible for setting and checking the 
        final values and option dependencies for all the options 
        just before the method run is executed.

        In practice, this is where the values are assigned and verified.
        """
        pass

    def run(self):
        """
        Semantically, runs 'python src/client/view.py SERVER_SOCKET' on the
        command line.
        """
        print(self.socket)
        errno = subprocess.call([sys.executable, 'src/client/view.py ' + self.socket])
        if errno != 0:
            raise SystemExit("Unable to run client GUI!")