试了很久都不行,麻烦各位帮我看一看。
表结构
Key Name Up(其中Key代表它的ID,Up代表它的上级)
如:
C1   系统信息 C
C1-1  籍贯 C1
C1-1-1 北京 C1-1
C1-1-1-1 朝阳区 C1-1-1
.
.(可能还有其他不同类型的信息)
.
B1    其他信息 B
B1-1      1    B1
B1-1-1    2    B1-1
B2-1      3    B1
B2-1-1    4    B2-1请问有没有类似的SQL可以输出这样的分类?

解决方案 »

  1.   

    *
    标题:查询各节点的父路径函数
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)  
    时间:2008-05-12
    地点:广东深圳
    *//*
    原始数据及要求结果如下:
    --食品 
      --水果 
        --香蕉 
        --苹果    
      --蔬菜 
        --青菜
    id          pid         name                 
    ----------- ----------- -------------------- 
    1           0           食品
    2           1           水果
    3           1           蔬菜
    4           2           香蕉
    5           2           苹果
    6           3           青菜要求得到各节点的父路径即如下结果:
    id  pid name  路径                         
    --- --- ----- ---------------
    1   0   食品  食品
    2   1   水果  食品,水果
    3   1   蔬菜  食品,蔬菜
    4   2   香蕉  食品,水果,香蕉
    5   2   苹果  食品,水果,苹果
    6   3   青菜  食品,蔬菜,青菜 
    */create table tb (id int , pid int , name nvarchar(20)) 
    insert into tb values(1 , 0 , '食品')
    insert into tb values(2 , 1 , '水果')
    insert into tb values(3 , 1 , '蔬菜')
    insert into tb values(4 , 2 , '香蕉')
    insert into tb values(5 , 2 , '苹果')
    insert into tb values(6 , 3 , '青菜')
    go--查询各节点的父路径函数
    create function f_pid(@id int) returns varchar(100)
    as
    begin
      declare @re_str as varchar(100)
      set @re_str = ''
      select @re_str = name from tb where id = @id
      while exists (select 1 from tb where id = @id and pid <> 0)
        begin
          select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
        end
      return @re_str
    end
    goselect * , dbo.f_pid(id) 路径 from tb order by iddrop table tb
    drop function f_pidSQL code
    /*
    标题:查询所有节点及其所有子节点的函数
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2009-04-12
    地点:广东深圳
    */--生成测试数据 
    create table tb(id varchar(10),pid varchar(10)) 
    insert into tb select 'a', null 
    insert into tb select 'b', 'a' 
    insert into tb select 'c', 'a' 
    insert into tb select 'd', 'b' 
    insert into tb select 'e', 'b' 
    insert into tb select 'f', 'c' 
    insert into tb select 'g', 'c' 
    go --创建用户定义函数 
    create function f_getchild(@id varchar(10)) returns varchar(8000) 
    as 
    begin 
      declare @i int , @ret varchar(8000) 
      declare @t table(id varchar(10) , pid varchar(10) , level int) 
      set @i = 1 
      insert into @t select id , pid , @i from tb where id = @id 
      while @@rowcount <> 0 
      begin 
        set @i = @i + 1 
        insert into @t select a.id , a.pid , @i from tb a , @t b where a.pid = b.id and b.level = @i - 1
      end 
      select @ret = isnull(@ret , '') + id + ',' from @t 
      return left(@ret , len(@ret) - 1)
    end 
    go --执行查询 
    select id , children = isnull(dbo.f_getchild(id) , '') from tb group by id
    go --输出结果 
    /* 
    id         children     
    ---------- -------------
    a          a,b,c,d,e,f,g
    b          b,d,e
    c          c,f,g
    d          d
    e          e
    f          f
    g          g(所影响的行数为 7 行)*/ --删除测试数据 
    drop function f_getchild 
    drop table tb
      

  2.   

    给你个例子 参照动动脑自己改改DECLARE  @tb Table([id] int,[col1] varchar(8),[col2] int)
    insert @tb
    select 1,'河北省',0 union all
    select 2,'邢台市',1 union all
    select 3,'石家庄市',1 union all
    select 4,'张家口市',1 union all
    select 5,'南宫',2 union all
    select 6,'坝上',4 union all
    select 7,'任县',2 union all
    select 8,'清河',2 union all
    select 9,'河南省',0 union all
    select 10,'新乡市',9 union all
    select 11,'aaa',10 union all
    select 12,'bbb',10;with t as(
    select * from @tb where col1='河北省'
    union all
    select a.* from @tb a  ,t where a.col2=t.id)
    select * from t
    /*
    (12 行受影响)
    id          col1     col2
    ----------- -------- -----------
    1           河北省      0
    2           邢台市      1
    3           石家庄市     1
    4           张家口市     1
    6           坝上       4
    5           南宫       2
    7           任县       2
    8           清河       2(8 行受影响)
    */
      

  3.   

    SQL Server 中树形表数据的处理总结 
    http://blog.csdn.net/xys_777/archive/2010/06/15/5672481.aspx
      

  4.   

    SQL Server BOM数据的处理http://blog.csdn.net/htl258/category/582950.aspx
      

  5.   

    其实就是给出一个键值 然后返回该键值下的所有子类
    因为我使用的是SQLITE数据库 好像不可以建立FUNCTION 所以这个问题一直很纠结~