表task,字段id,name
id的数据如下形式:
1
1.1
1.1.1
1.1.1.2
1.2
1.2.1
...
10
10.1
10.1.1
10.2.1
10.2.1.1
10.2.1.1.1
10.2.1.1.2
.......注:“.”标识父子的关系。
现在通过id查询树结构的效果,并且知道此节点是否为叶子节点leaf。。??

解决方案 »

  1.   

    记得有个connect_by_isleaf,来判断当前行是不是叶子节点。
      

  2.   

    我整理了N年的常用SQL宝典。苍天啊,偶恨GHOST!网上找了只文章,LZ看看,希望能对你有帮助。
      

  3.   

    http://blog.chinaunix.net/u2/67157/showart_690324.html好久没用到了,找只树形表也练习练习。
      

  4.   

    SQL> col areaname format a20
    SQL> col 叶子节点标志 format 0
    SQL> SELECT * FROM (
      2  SELECT lpad(areaname,4*LEVEL,' ') areaname,connect_by_isleaf 叶子节点标志 F
    ROM area
      3  START WITH parentareaid=-1 CONNECT BY parentareaid=PRIOR areaid)
      4  WHERE ROWNUM <=15;AREANAME             叶子节点标志
    -------------------- ------------
    浙江                            0
      杭州市                        0
          上城区                    1
          下城区                    1
          江干区                    1
          拱墅区                    1
          西湖区                    1
          滨江区                    1
          萧山区                    1
          余杭区                    1
          桐庐县                    1AREANAME             叶子节点标志
    -------------------- ------------
          淳安县                    1
          建德市                    1
          富阳市                    1
          临安市                    1已选择15行。SQL>
      

  5.   

    select id,name,(case when id='id' then 1 else 0 end) isLeaf from task where id like 'id%' order by id
      

  6.   

    ---利用instr函数统计你的节点的字串在表中(ID)字段中标
    ---出现的次数,如果只有一条记录,不就证明他是叶子节点Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 
    Connected as scott
    SQL> 
    SQL> with tab as(
      2  select '1' id from dual union all
      3  select '1.1' id from dual union all
      4  select '1.1.1'  id from dual union all
      5  select '1.1.1.2' id from dual union all
      6  select '1.2'  id from dual union all
      7  select '1.2.1'  id from dual)
      8  select tab2.*
      9    from tab tab1, tab tab2
     10   where instr(tab2.id, tab1.id) > 0
     11     and tab1.id = '1.1.1'
     12  /ID
    -------
    1.1.1
    1.1.1.2SQL> 
      

  7.   

    请问5楼,connect_by_isleaf 能判断是否为叶子节点,那么判断一个节点下面又多少个叶子节点,有这方面的函数否?
      

  8.   

    select id,name,(case when length(substr(id,1,instr(id,'.',1)-1))>length(substr('給定的ID值',1,instr('給定的ID值','.',1)-1)) then 1 else 0 end) isLeaf from task where id like substr('給定的ID值',1,instr('給定的ID值','.',1)-1)+'%' order by id若該節點是叶子节点,那么第三列的值全為0
      

  9.   

    select 
     id_ ,
     name_ ,
     (select count(id_) from  task where t.id_= substr(id_,0,INSTR((id_),'.',-1)-1) )   as  leaf,connect_by_isleaf from  task t  
       start with  t.id_ ='5' connect by prior
       t.id_= substr(t.id_,0,INSTR((t.id_),'.',-1)-1) order by t.id_
      

  10.   

    --页子节点总数
    WITH tb AS
     (SELECT LEVEL, task.*, connect_by_isleaf isleaf
        FROM task
      CONNECT BY PRIOR id = substr(id, 1, instr(id, '.', -1, 1) - 1)
       START WITH id = '1')
    SELECT tb.id, tb.name, isleaf, decode(isleaf, 1, 0, leafcount) leafcount
      FROM (SELECT b.id, SUM(isleaf) leafcount
              FROM tb a, task b
             WHERE a.id LIKE b.id || '%'
             GROUP BY b.id) c,tb
     WHERE c.id = tb.id
     ORDER BY tb.id;
      

  11.   


    和我先做的效果是一样的:
    比如:id为1 为一条数据(id为1下有1.1,1.2)
          id为1.1和1.2重复2次,如果id为1.1,1.2下各有三条数据。如1.1.1,1.1.2,1.1.3则这些数据重复3次。
    这样是不能准确算出多少叶子节点,并且"."越多,数据重复越多。