刚才的脚本有点问题,这个为准------------建立子表
create table fjb
(id int ,name varchar(10),pid int ,ppid  int )insert into fjbselect 10, 'aa1','',1 union all
select 20, 'bb1',10,1 union all
select 30, 'cc1',10,1 union all
select 40, 'dd1',10,2 union all
select 50, 'ff1',20,2 union all
select 60, 'hh1',20,3---建立父表create table zjb(ppid int , name varchar(max) ,pppid int )
insert into zjb
select 1,'aa','' union all
select 2,'bb',2 union all
select 3,'cc',2 union all
select 4,'dd',3 union all
select 5,'ee',3 union all
select 6,'ff',3 

解决方案 »

  1.   

    合并成 id name pid,这样就行
      

  2.   

    insert into zjb
    select 1,'aa','' union all
    select 2,'bb',2 union all
    select 3,'cc',2 union all 这三行什么意思?
    第一行没有父id
    第二行父ID为其本身,也就是没有父ID, 为什么同一种意义有两种不同的表示?
    第三行是第二行的子。同一种关系的约定,不要有多种表示,这样会给以后造成混乱
      

  3.   

    那个有点问题,create table zjb(ppid int , name varchar(max) ,pppid int )
    insert into zjb
    select 1,'aa','' union all
    select 2,'bb',1 union all
    select 3,'cc',1 union all
    select 4,'dd',2 union all
    select 5,'ee',2 union all
    select 6,'ff',2 以这个为准,能否给个思路,不知道你明白我的意思没?
      

  4.   


    create table fjb 
    (id int ,name varchar(10),pid int ,ppid  int ) 
    goinsert into fjb select 10, 'aa1','',1 union all 
    select 20, 'bb1','',1 union all 
    select 30, 'cc1','',1 union all 
    select 40, 'dd1','',1 union all 
    select 50, 'ff1','',1 union all 
    select 60, 'hh1','',2 
    gopid 全是空吗??
      

  5.   

    fjb表里的 ppid 是 pid 父级????  
      

  6.   

    -->测试数据
    create table zjb(ppid int , name varchar(max) ,pppid int )
    insert into zjb
    select 1,'aa','' union all
    select 2,'bb',1 union all
    select 3,'cc',1 union all
    select 4,'dd',2 union all
    select 5,'ee',2 union all
    select 6,'ff',2 create table fjb
    (id int ,name varchar(10),pid int ,ppid  int )insert into fjbselect 10, 'aa1','',1 union all
    select 20, 'bb1',10,1 union all
    select 30, 'cc1',10,1 union all
    select 40, 'dd1',10,2 union all
    select 50, 'ff1',20,2 union all
    select 60, 'hh1',20,3 -->测试
    --fjb 父子分组
    ;WITH Args(id,NAME,p_id,pp_id)  AS 
    (
    SELECT id ,NAME,pid,ppid FROM fjb WHERE pid = ''
    UNION ALL 
    SELECT fjb.id ,fjb.NAME,fjb.pid,fjb.ppid FROM fjb ,Args WHERE Args.id  = fjb.pid
    ),
    --zjb父子分组
    Args1(id,NAME,p_id) AS 
    (
    SELECT ppid,NAME,pppid FROM zjb WHERE pppid  =''
    UNION ALL 
    SELECT zjb.ppid,zjb.NAME,zjb.pppid FROM  zjb ,Args1 WHERE zjb.pppid = Args1.id
    ),Args2(id,NAME,P_id) AS 
    (
    SELECT id,NAME,pp_id FROM Args 
    UNION ALL 
    SELECT id,NAME,p_id FROM Args1
    )
    ,
    Args3 AS 
    (
    SELECT id,NAME,p_id FROM Args2 WHERE id ='2'
    UNION ALL 
    SELECT Args2.id,Args2.NAME,Args2.p_id FROM Args2 ,Args3 WHERE args3.id = args2.p_id 
    )
    SELECT *FROM Args3;-->测试结果
    /*                                                                                                                                                                                                                                     id        NAME  p_id
    ------- ------ -----------
    2 bb 1
    40 dd1 2
    50 ff1 2
    4 dd 2
    5 ee 2
    6 ff 2
    */