问个sql数据库的问题
有个 test 表,结构如下:id   name   pid
1    一部    0
2    二部    0
3    北京    1
4    上海    2
5    广州    2
6    深圳    1id为自增长类型,pid是指的 父id要求用一条sql语句实现以下结果:(父节点下跟着显示出子节点)id   name   pid
1    一部    0
3    北京    1
6    深圳    1
2    二部    0
4    上海    2
5    广州    2请大侠们多多指教

解决方案 »

  1.   


    pid=1的记录是 id=1的子节点
    pid=2的记录是 id=2的子节点第一条记录:id=1 , pid=0  (第一个根节点)
    第二条记录:id=3 , pid=1  (第一个根节点的第一个子节点)
    第三条记录:id=6 , pid=1  (第一个根节点的第二个子节点)第四条记录:id=2 , pid=0  (第二个根节点)
    第五条记录:id=4 , pid=2  (第二个根节点的第一个子节点)
    第六条记录:id=5 , pid=2  (第二个根节点的第二个子节点)要求每个根节点后跟着子节点若干个
      

  2.   

    -- 先把SQL贴上:
    drop table test;create table test(
    id int, 
    name varchar(10),
    pid int
    );INSERT INTO test(id,name,pid)
    SELECT
    1, '一部', 0 UNION ALL SELECT
    2, '二部', 0 UNION ALL SELECT
    3, '北京', 1 UNION ALL SELECT
    4, '上海', 2 UNION ALL SELECT
    5, '广州', 2 UNION ALL SELECT
    6, '深圳', 1;
      

  3.   

    --drop table test;create table test(
    id int, 
    name varchar(10),
    pid int
    );INSERT INTO test(id,name,pid)
    SELECT
    1, '一部', 0 UNION ALL SELECT
    2, '二部', 0 UNION ALL SELECT
    3, '北京', 1 UNION ALL SELECT
    4, '上海', 2 UNION ALL SELECT
    5, '广州', 2 UNION ALL SELECT
    6, '深圳', 1;
    SELECT * FROM TEST  T ORDER BY ISNULL((SELECT ID FROM TEST WHERE ID=T.PID),ID),idid          name       pid         
    ----------- ---------- ----------- 
    1           一部         0
    3           北京         1
    6           深圳         1
    2           二部         0
    4           上海         2
    5           广州         2(所影响的行数为 6 行)
      

  4.   

    SELECT t2.id, t2.pid,t2.name
     FROM test t1 right join test t2 on t1.id=t2.pid
    ORDER BY isnull(t1.id,t2.id),t2.id;
      

  5.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([id] int,[name] varchar(4),[pid] int)
    insert [tb]
    select 1,'一部',0 union all
    select 2,'二部',0 union all
    select 3,'北京',1 union all
    select 4,'上海',2 union all
    select 5,'广州',2 union all
    select 6,'深圳',1--------------------------------查询开始------------------------------
    select * from tb a order by isnull((select id from tb where id=a.pid),id),id
    /*
    id          name pid
    ----------- ---- -----------
    1           一部   0
    3           北京   1
    6           深圳   1
    2           二部   0
    4           上海   2
    5           广州   2(6 行受影响)*/
      

  6.   

    if object_id('test') is not null drop table test
    go
    create table test(id int,typeName varchar(10),pid int)
    insert test
    select 1,'一部',0 union all
    select 2,'二部',0 union all
    select 3,'北京',1 union all
    select 4,'上海',2 union all
    select 5,'广州',2 union all
    select 6,'深圳',1 select * from test a where id in (
    select a.id from  test  b,f_getC(a.id)
    ) and a.pid=0
    select a.* from test  a,f_getC(0) b  where a.id=b.id order by b.sort  
    /*
    id          typeName   pid         
    ----------- ---------- ----------- 
    1           一部         0
    3           北京         1
    6           深圳         1
    2           二部         0
    4           上海         2
    5           广州         2(所影响的行数为 6 行)
    */if object_id('f_getC') is not null drop function f_getC
    go
    create function f_getC(@id int)  
    returns @re table(id int,level int,sort varchar(100))  
    as  
    begin
        declare @l int  
        set @l=0  
        insert @re select @id,@l  ,right('00'+ltrim(@id),3)
        while @@rowcount>0
        begin  
            set @l=@l+1
            insert @re select a.id,@l,b.sort+'-'+right('00'+ltrim(a.id),3) from test as a,@re as b  
    where b.id=a.pid and b.level=@l-1
        end
        return  
    end  
    go