Oracle树形结构数据查询(二)

利用start with   connect by  查询子节点和父节点数据显示为树状形


前段时间做了一个根据子节点和父节点查询出树状型

表数据:


要实现的结果:



实现的sql:

[sql]  view plain  copy
  1. select rpad('---', (level - 1) * 3, '---') || name as name, id  
  2.   from t_nscreen_region  
  3.  start with id = 1  
  4. connect by nocycle prior id = parent_id   
order SIBLINGS BY id;



其中函数理解:
[plain]  view plain  copy
  1. 1.PRIOR   
  2.    阶层查询的CONNECY BY condition的条件式需要用到PRIOR来指定父节点  
  3. 2.CONNECT BY  
  4.    通过CONNECT BY子句定义子节点和父节点的关系  
  5. 3.start with   
  6.    通过start with 指定根节点(不指定start with会出现重复记,不指定NOCYCLE没事)  
  7. 4.LEVEL   
  8.    通过LEVEL虚拟列表示节点的关系  
  9. 5.rpad(string,padded_length,[pad_string])函数  
  10.    从右边对字符串使用指定的字符进行填充  
  11.    string:表示要追加的字符,  
  12.    padded_length:表示追加后的长度  
  13.    pad_string:表示string字符长度不够padded_length时,取pad_string的字符,默认为空格  
  14.   
  15. 6.lpad( string1, padded_length, [ pad_string ] )函数  
  16.    从左边对字符串使用指定的字符进行填充  
  17.    string1:源字符串  
  18.    padded_length:最终返回的字符串的长度,如果最终返回的字符串的长度比源字符串的小,那么此函数实际上对源串进行截断处理  
  19.    pad_string:用于填充的字符,可以不填,默认为空字符  
  20. 7.order SIBLINGS BY id :按id进行排序  

猜你喜欢

转载自blog.csdn.net/qq649954944/article/details/79908132