现有一树结构表,结构为
functionId 标识,
functionCode 代号(如ZL001,ZL001001以两位缩写字母开头每一层加三位数字),
functionName 名称,
priority 显示顺序(整数) ,
parentid,父结点标识
现要遍历所有数据以层次结构显示,同层以显示顺序升序
如表中有数据:
'id1','ZL001','功能1',2,0;
'id2','ZL001001','功能2',3,'id1'
'id3','ZL001002','功能3',2,'id1';
'id4','ZL002','功能4',1,0;结果为:
'id4','ZL002','功能4',1,0;
'id1','ZL001','功能1',2,0;
'id3','ZL001002','功能3',2,'id1';
'id2','ZL001001','功能2',3,'id1'

解决方案 »

  1.   

    select * from tab start with parentid=0 Connect by prior functionId=parentid order by parentid,priority
      

  2.   

    SQL> select * from test;FUNCTION FUNCTION FUNCTIONNAME           PRIORITY PARENTID
    -------- -------- -------------------- ---------- --------
    id1      ZL001    功能1                         2 0
    id2      ZL001001 功能2                         3 id1
    id3      ZL001002 功能3                         2 id1
    id4      ZL002    功能4                         0 0SQL> select t.*,level from test t
      2  start with parentid = '0'
      3  connect by parentid = prior functionid
      4  order siblings by priority;FUNCTION FUNCTION FUNCTIONNAME           PRIORITY PARENTID      LEVEL
    -------- -------- -------------------- ---------- -------- ----------
    id4      ZL002    功能4                         0 0                 1
    id1      ZL001    功能1                         2 0                 1
    id3      ZL001002 功能3                         2 id1               2
    id2      ZL001001 功能2                         3 id1               2
      

  3.   

    补充:waterfirer(水清) 的结果跟我的是不相同的,再加一层就可以看出来了,楼主看自己想要那种结果SQL> select * from test;FUNCTION FUNCTIONCODE         FUNCTI   PRIORITY PARENTID
    -------- -------------------- ------ ---------- --------
    id1      ZL001                功能1           2 0
    id2      ZL001001             功能2           3 id1
    id3      ZL001002             功能3           2 id1
    id5      ZL001001001          功能5           1 id2
    id6      ZL001001002          功能6           3 id2
    id7      ZL001002003          功能7           2 id3
    id8      ZL001002004          功能8           0 id3
    id4      ZL002                功能4           0 0已选择8行。SQL> select t.* from test t
      2  start  with parentid = '0'
      3  connect by parentid = prior functionid
      4  order by parentid,priority;FUNCTION FUNCTIONCODE         FUNCTI   PRIORITY PARENTID
    -------- -------------------- ------ ---------- --------
    id4      ZL002                功能4           0 0
    id1      ZL001                功能1           2 0
    id3      ZL001002             功能3           2 id1
    id2      ZL001001             功能2           3 id1
    id5      ZL001001001          功能5           1 id2
    id6      ZL001001002          功能6           3 id2
    id8      ZL001002004          功能8           0 id3
    id7      ZL001002003          功能7           2 id3已选择8行。SQL> select t.* from test t
      2  start  with parentid = '0'
      3  connect by parentid = prior functionid
      4  order  siblings by priority;FUNCTION FUNCTIONCODE         FUNCTI   PRIORITY PARENTID
    -------- -------------------- ------ ---------- --------
    id4      ZL002                功能4           0 0
    id1      ZL001                功能1           2 0
    id3      ZL001002             功能3           2 id1
    id8      ZL001002004          功能8           0 id3
    id7      ZL001002003          功能7           2 id3
    id2      ZL001001             功能2           3 id1
    id5      ZL001001001          功能5           1 id2
    id6      ZL001001002          功能6           3 id2已选择8行。
      

  4.   

    想了想,觉得order by parentid,priority还是不对,这里parentid刚好是有规律的,如果parentid是无规律的,这样排序的层次就乱了
      

  5.   

    duanzilin(寻)说的对,我就是看到了楼主的parentid有规律才那样写的,刚好可以满足楼主要求:)duanzilin(寻)的siblings是什么意思,我这怎么不起作用,我用的是9i
      

  6.   

    The SIBLINGS keyword is valid only if you also specify the hierarchical_query_clause (CONNECT BY). ORDER SIBLINGS BY preserves any ordering specified in the hierarchical query clause and then applies the order_by_clause to the siblings of the hierarchy.SIBLINGS只能在connect by 语句结构中使用,用在同等级下的各个节点的排序,默认状态下是不排序的
    ------------------
    我用的也是9i版本,是好用的啊
      

  7.   

    有能用标准SQL搞定的吗?
    ----------------------------
    答案是不能的 ,就拿sql server2000来说,要用纯sql语句实现这样的功能是不可能的;而用存储过程,各种数据库也有不同的标准,所以这个问题没有标准sql