Oracle 树结构查询

来源:互联网 发布:人工智能程序原理 编辑:程序博客网 时间:2024/05/21 05:41

/** 首先,自我检讨一下,在没完全了解树结构查询语句就开始用,结果一直在其他地方找问题.../

树结构查询语句并不复杂:

select .. from.. where ...[conditon] start with ...[condition] connect by prior...[condition]

例如 :

         select  dm, mc, pid            from tree           where ...[condition]      start with dm = '根节点条件'connect by prior dm = pid 

其中:

  • prior :阶层查询的 connect by condition 的条件式需要用到PRIOR来指定父节点,作为运算符,prior 和加(+)减(-)运算的优先级相同;
  • start with:指定阶层的根;
  • connect by:指定父节点与子节点的关系(其中 prior 运算符来指定父节点的字段)
  • connect_by_root:查询指定根的阶层数据, 例:

select last_name "employee", connect_by_root last_name "manager",
level-1 "pathlen", sys_connect_by_path(last_name, '/') "path"
from employees
where level > 1 and department_id = 110
connect by prior employee_id = manager_id;
employee manager pathlen path
--------------- ------------ ---------- -----------------------------------
higgins kochhar 1 /kochhar/higgins
gietz kochhar 2 /kochhar/higgins/gietz
gietz higgins 1 /higgins/gietz
higgins king 2 /king/kochhar/higgins

gietz king 3 /king/kochhar/higgins/gietz

错误:

出现的错误就是在 connect by 的两个条件中,一个没有加 prior运算符 

0 0
原创粉丝点击