oralce树形数据结构构建

oralce树形数据结构构建

 

1,单表:

      SELECT * FROM opm_menufolder t START WITH t.parentid is null  

CONNECT BY PRIOR t.id = t.parentid  ORDER SIBLINGS BY t.parentid 

START WITH递归的开始

connect by 是一张表里的递归。

 

2,多表:

 

union all 上下合并(join是左右合并),如果是多张表在的树形结构可以先用union查出一个上下合并的结果,然后基于这个结果集(看成一张表)

扫描二维码关注公众号,回复: 271038 查看本文章

在这一张表的基础上用单表的迭代,构建树形结构(外层的字段名用里面的别名)

union all 下面的总是子节点

ORDER SIBLINGS BY parentid  完全按树形排序

ORDER  BY parentid  分类排序

 

 

select * from 

(   

   /*select '权限树' name,'0' id,'-1' parentid from dual */

    select t.name name,t.id  id,t.parentid  parentid from opm_menufolder t 

   union all select o.name name,o.id id,o.menufolderid   from opm_menuitem o

start with parentid is null connect by prior id=parentid

 

select * from 

(   

   /*select '权限树' name,'0' id,'-1' parentid from dual */

    select t.name name,t.id  id,t.parentid  parentid from opm_menufolder t 

   union all select o.name name,o.id id,o.menufolderid   from opm_menuitem o

start with parentid is null connect by prior id=parentid ORDER SIBLINGS BY parentid

 

 

A:select * from 

(   

   /*select '权限树' name,'0' id,'-1' parentid from dual */

    select t.name name,t.id  id,t.parentid  parentid from opm_menufolder t 

   union all select o.name name,o.id id,o.menufolderid   from opm_menuitem o

   union all select l.name name,l.id id,l.menuitemid   from opm_menuitemlimit l

start with parentid is null connect by prior id=parentid ORDER SIBLINGS BY parentid

 

 

另一种:这种数据条数不一致,没有connect by 的 中间断层的也会查出来,(比如opm_menuitemlimit(按钮数据)中有按钮,也挂在了一个opm_menuitem(页面数据)中的页面下,

但是在opm_menuitem表中找不到这个id的页面数据(180????15010401150104))

所以如果要在with as中用递归的话用块中自调的方法(如1,2),否则还是只用于查询块(3中数据多于A的数据就是因为把断层数据算进去)

递归函数写法(单表递归)

 CTE(id,name,parentid)这个里面的是列名

1 with CTE(id) AS(

  SELECT t.ID id FROM opm_menufolder t where t.id=100000/*//查询出当前省的ID*/

  union all                                             /*//显示当前级别以下的所有有关的数据*/

  select opm_menufolder.ID  id from CTE    /*//查找出属于当前省的市,以及县的ID*/

  inner join opm_menufolder on CTE.id=opm_menufolder.parentid/*//递归查询*/

  ) select * from CTE

 

 

2  with CTE(id,name) AS(

  SELECT t.ID id,t.name name  FROM opm_menufolder t where t.parentid is null/*//查询出当前省的ID*/

  union all                                             /*//显示当前级别以下的所有有关的数据*/

  select opm_menufolder.ID  id ,opm_menufolder.name  name from CTE    /*//查找出属于当前省的市,以及县的ID*/

  inner join opm_menufolder on CTE.id=opm_menufolder.parentid/*//递归查询*/

  ) select * from CTE

 

 

3,with CTE(id,name,parentid) AS(

     select t.name name,t.id  id,t.parentid  parentid from opm_menufolder t 

   union all select o.name name,o.id id,o.menufolderid   from opm_menuitem o

   union all select l.name name,l.id id,l.menuitemid   from opm_menuitemlimit l

  ) select * from CTE ORDER  BY parentid

 

    select t.name name,t.id  id,t.parentid  parentid from opm_menufolder t (这个和上面的一样,此时的with as 只是查询块)

   union all select o.name name,o.id id,o.menufolderid parentid  from opm_menuitem o

   union all select l.name name,l.id id,l.menuitemid  parentid from opm_menuitemlimit l ORDER BY parentid

****************************************************************

注意:with 函数前后要加;

 

猜你喜欢

转载自yuhuiblog6338999322098842.iteye.com/blog/2310709