SQL树结构

SIBLINGS 是兄弟姐妹的意思,那么ORDER SIBLINGS BY的意思就是在兄弟姐妹之间的排序,和order by所表示的含义绝对不同,针对树状sql,我觉得ORDER SIBLINGS BY更有意义,树状sql查询出来的结果本身就是按照层次(hierarchy)结构排好序的,而加上关键字SIBLINGS 其实是在hierarchy内部进行排序。


You have to be careful when trying to order the rows of output in a hierarchical
query. By default, if you omit the ORDER BY clause altogether, the query attempts
to sort rows in an order that’s reflective of the hierarchy. Display will start first with
a LEVEL 1 row. If that row is superior to any LEVEL 2 rows, those rows will display
next before another LEVEL 1 row displays. The same approach is taken at LEVEL
2, so that rows will display down to leaf node levels before the next rows show at
the higher levels. The result is a display that is meaningful to the hierarchy. But if
you try to order these rows with the ORDER BY clause, you’ll create a syntactically
correct statement that probably doesn’t help you much

--===================================

上面是047教材中对ORDER SIBLINGS BY的描述,看的有点晕,下面看一个例子来理解一下SIBLINGS吧:

SQL> select * from t_tree_query;

NAME MID ID
---------- ---------- ----------
level999 0 1
level2 1 2
level3 2 3
level4 3 4
level5 4 5
level6_7 5 6
level6_6 5 7
level5 4 8
level6 8 9
level4 3 10
level5_12 10 11

NAME MID ID
---------- ---------- ----------
level5_11 10 12
level6 12 13

13 rows selected.

SQL>

表t_tree_query里的数据如上,字段mid是id的parent,name看成是id对应的名字,下面是一个树状sql的查询结果,目的是求mid=0也就是父亲id=0的所有孩子id的关系:

SQL> select name,level,mid,id ,lpad(' ',level*2)||id level_id,SYS_CONNECT_BY_PATH(id,'/') path
2 ,CONNECT_BY_ROOT id root_id
3 from t_tree_query
4 start with mid in (0)
5 connect by prior id = mid
6 ;

NAME LEVEL MID ID LEVEL_ID PATH ROOT_ID
---------- ---------- ---------- ---------- -------------------- --------------- ----------
level999 1 0 1 1 /1 1
level2 2 1 2 2 /1/2 1
level3 3 2 3 3 /1/2/3 1
level4 4 3 4 4 /1/2/3/4 1
level5 5 4 5 5 /1/2/3/4/5 1
level6_7 6 5 6 6 /1/2/3/4/5/6 1
level6_6 6 5 7 7 /1/2/3/4/5/7 1
level5 5 4 8 8 /1/2/3/4/8 1
level6 6 8 9 9 /1/2/3/4/8/9 1
level4 4 3 10 10 /1/2/3/10 1
level5_12 5 10 11 11 /1/2/3/10/11 1

NAME LEVEL MID ID LEVEL_ID PATH ROOT_ID
---------- ---------- ---------- ---------- -------------------- --------------- ----------
level5_11 5 10 12 12 /1/2/3/10/12 1
level6 6 12 13 13 /1/2/3/10/12/13 1

13 rows selected.

SQL>

查询的结果是按照mid和id的层次关系排好了序,但是我们希望在具有相同父亲的孩子之间按照name来排序,这样加上order siblings by name子句的查询结果如下:

SQL> select name,level,mid,id ,lpad(' ',level*2)||id level_id,SYS_CONNECT_BY_PATH(id,'/') path
2 ,CONNECT_BY_ROOT id root_id
3 from t_tree_query
4 start with mid in (0)
5 connect by prior id = mid
6 order siblings by name
7 ;

NAME LEVEL MID ID LEVEL_ID PATH ROOT_ID
---------- ---------- ---------- ---------- -------------------- --------------- ----------
level999 1 0 1 1 /1 1
level2 2 1 2 2 /1/2 1
level3 3 2 3 3 /1/2/3 1
level4 4 3 4 4 /1/2/3/4 1
level5 5 4 5 5 /1/2/3/4/5 1
level6_6 6 5 7 7 /1/2/3/4/5/7 1
level6_7 6 5 6 6 /1/2/3/4/5/6 1
level5 5 4 8 8 /1/2/3/4/8 1
level6 6 8 9 9 /1/2/3/4/8/9 1
level4 4 3 10 10 /1/2/3/10 1
level5_11 5 10 12 12 /1/2/3/10/12 1

NAME LEVEL MID ID LEVEL_ID PATH ROOT_ID
---------- ---------- ---------- ---------- -------------------- --------------- ----------
level6 6 12 13 13 /1/2/3/10/12/13 1
level5_12 5 10 11 11 /1/2/3/10/11 1

13 rows selected.

SQL>

观查一下name中level6_6和level6_7的位置发现他们的顺序发生了变化,他们的父亲都是mid=5,在不加order siblings by name之前,id=6在id=7之前,按照name排序之后,发现id=7在id=6前面了,大家也可以观查mid=10时id=11,12,13时的变化,发现加上siblings 之后父亲12会带着孩子13同时和11的顺序颠倒了。

注意siblings 的作用仅仅是兄弟姐妹之间的排序,不管加上siblings 与否,level999的位置始终是在第一位的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值