sql中ORDER SIBLINGS BY排序的含义
来源:互联网 发布:免费开源的cms 编辑:程序博客网 时间:2024/05/16 14:03
SIBLINGS 是兄弟姐妹的意思,那么ORDER SIBLINGS BY的意思就是在兄弟姐妹之间的排序,和orderby所表示的含义绝对不同,针对树状sql,我觉得ORDER SIBLINGSBY更有意义,树状sql查询出来的结果本身就是按照层次(hierarchy)结构排好序的,而加上关键字SIBLINGS其实是在hierarchy内部进行排序。
You have to be careful when trying to order the rows of output in ahierarchical
query. By default, if you omit the ORDER BY clause altogether, thequery 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, thoserows will display
next before another LEVEL 1 row displays. The same approach istaken at LEVEL
2, so that rows will display down to leaf node levels before thenext rows show at
the higher levels. The result is a display that is meaningful tothe hierarchy. But if
you try to order these rows with the ORDER BY clause, you’ll createa 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)||idlevel_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来排序,这样加上ordersiblings by name子句的查询结果如下:
SQL> select name,level,mid,id ,lpad(' ',level*2)||idlevel_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,在不加ordersiblings byname之前,id=6在id=7之前,按照name排序之后,发现id=7在id=6前面了,大家也可以观查mid=10时id=11,12,13时的变化,发现加上siblings之后父亲12会带着孩子13同时和11的顺序颠倒了。
注意siblings 的作用仅仅是兄弟姐妹之间的排序,不管加上siblings 与否,level999的位置始终是在第一位的。
- sql中ORDER SIBLINGS BY排序的含义
- sql中ORDER SIBLINGS BY排序的含义
- oracle ORDER SIBLINGS BY
- Oracle递归层次查询-亲兄弟间的排序ORDER SIBLINGS BY
- sql server中order by排序的用法
- sql中order by与索引排序的区别
- sql中order by对null值的排序
- SQL中先分组再组内排序再输出的ROW_NUMBER() OVER(partition by..order by..)
- sql 中 order by
- SQL语法中Order by的用法
- SQL查询排序ORDER BY
- SQL 经典排序 Order By
- SQL ORDER BY不规则排序
- SQL ORDER BY(排序)
- SQL 的order by
- SQL语句中order by两个字段同时排序
- sql语句中order by 多个字段同时排序的应用
- SQL中order by 、group by 、having的用法区别
- 剑指offer系列源码-两个链表的第一个公共结点
- 取消线程接口pthread_cancel吐槽
- JAVA反射机制
- 用双缓存技术优化listview异步加载网络图片
- C++中 public,protected, private 访问标号小结
- sql中ORDER SIBLINGS BY排序的含义
- 神奇的mongo:找出某个集合中包含boolean数据的文档
- ListView删除Item的动画
- 转载-Oracle 11g Alert log 文件位置的问题
- 求三个数的最大 最小 中间值
- 博客虫资料分享
- 蛇形填数
- 动态内存分配
- a missing vtable usually means the first non-inline virtual member function has no definition.