sql中ORDER SIBLINGS BY排序的含义
没有加ORDER SIBLINGS BY的时候:
SELECT y.* FROM
(
SELECT
m.id,m.name,m.parent_id,m.image,m.memo,m.status ,
t.id
operate_id,t.name operate_name
FROM t_module m
LEFT JOIN
t_module_operate t_m ON m.id=t_m.module_id
LEFT JOIN t_operate t ON
t.id=t_m.operate_id
) y
START WITH parent_id=-1
CONNECT BY PRIOR id=parent_id
加了 ORDER SIBLINGS BY的时候:
SELECT y.* FROM
(
SELECT
m.id,m.name,m.parent_id,m.image,m.memo,m.status ,
t.id
operate_id,t.name operate_name
FROM t_module m
LEFT JOIN
t_module_operate t_m ON m.id=t_m.module_id
LEFT JOIN t_operate t ON
t.id=t_m.operate_id
) y
START WITH parent_id=-1
CONNECT BY PRIOR id=parent_id
ORDER SIBLINGS BY id
SIBLINGS 是兄弟姐妹的意思,那么ORDER SIBLINGS BY的意思就是在兄弟姐妹之间的排序,和order by所表示的含义绝对不同,针对树状sql,我觉得ORDER SIBLINGS BY更有意义,树状sql查询出来的结果本身就是按照层次(hierarchy)结构排好序的,而加上关键字SIBLINGS 其实是在hierarchy内部进行排序。
树型查询中按同一级别(level相同,即:同一父节点下的直接子节点)排序,不加则可不能保证顺序。
层次查询-亲兄弟间的排序ORDER SIBLINGS BY