TA: 
id Yid T1 T2  ...
1       s 2
2   1   22 22s
3       ss 12
4   2   ss2 2
...
无论 输入 1,2或4 都能 得到下列数据 
[id 和yid 有关联的数据 全部都取出来]
id Yid T1 T2  ...
1      
2   1
4   2
...

解决方案 »

  1.   

    不是; 
    table1: 
    id Yid T1  T2  ... 
    1      s   2 
    2  1   22  22s 
    3      ss  12 
    4  2   ss2 2 
    5  3   1   w
    6  4   2   q1问题是 请查找出 所有 id=1[或2或4或6] 有关系的数据 [Yid和id相同的也找出来]
    得到的答案是:
    id Yid T1  T2  ... 
    1      s   2 
    2  1   22  22s 
    4  2   ss2 2 
    6  4   2   q1如果 id=3[或5] 得到
    id Yid T1  T2  ... 
    3      ss  12 
    5  3   1   w
      

  2.   

    if object_id('table1')is not null drop table table1
    go
    create table table1(id int, Yid int,  T1 varchar(5),  T2 varchar(8)) 
    insert table1 select  1,  null,'s',  2 
    insert table1 select  2,  1  , 22 ,'22s' 
    insert table1 select  3,  null,'ss'  ,12 
    insert table1 select  4,  2  , 'ss2', 2 
    insert table1 select  5,  3  ,  1,  'w' 
    insert table1 select  6,  4  ,  2,  'q1' 
    select * from table1 where charindex(','+rtrim(id)+',',',1,2,4,6,')>0 or charindex(','+rtrim(yid)+',',',1,2,4,6,')>0
    /*id          Yid         T1    T2       
    ----------- ----------- ----- -------- 
    1           NULL        s     2
    2           1           22    22s
    4           2           ss2   2
    6           4           2     q1*/
    select * from table1 where charindex(','+rtrim(id)+',',',3,5,')>0 or charindex(','+rtrim(yid)+',',',3,5,')>0
    /*id          Yid         T1    T2       
    ----------- ----------- ----- -------- 
    3           NULL        ss    12
    5           3           1     w*/
      

  3.   

    不好意思,没有表达出我的意思...
    1,2,4,6;3,5 只是举例...   
    table1: 
    id Yid T1  T2  ... 
    1      s  2 
    2  1   22  22s 
    3      ss  12 
    4  2   ss2 2 
    5  3   1  w 
    6  4   2  q1 
    ...
    52 5  null null 
    ...
    99 52  1  null
    ...就是取 上下手 数据啊大哥...郁闷了...
    id 是当前的主键;而yid 是 上手的主键;
    现在 就是想把 id=某个值时,把它一连串的数据都提取出来!!
      

  4.   


    select a.* from table1 a ,(select c.id,c.Yid  from table1 c left join table1 d on c.yid=d.id) b
    where a.id=b.id or a.id=b.yid我自己瞎琢磨的  不知道行不行
      

  5.   

    你这是个树吧?
    把下面两个函树结合起来用就行了./*
    标题:查询指定节点及其所有子节点的函数
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有子节点的函数
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @id , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.id , @level
        from tb a , @t_Level b
        where a.pid = b.id and b.level = @level - 1
      end
      return
    end
    go--调用函数查询001(广东省)及其所有子节点
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市
    003  001  深圳市
    004  002  天河区
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 10 行)
    */--调用函数查询002(广州市)及其所有子节点
    select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    002  001  广州市
    004  002  天河区(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有子节点
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    003  001  深圳市
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 7 行)
    */drop table tb
    drop function f_cid/*
    标题:查询指定节点及其所有父节点的函数
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有父节点的函数
    create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
    as
    begin
      insert into @t_level select @id
      select @id = pid from tb where id = @id and pid is not null
      while @@ROWCOUNT > 0
      begin
        insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
      end
      return
    end
    go--调用函数查询002(广州市)及其所有父节点
    select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有父节点
    select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    003  001  深圳市(所影响的行数为 2 行)
    */--调用函数查询008(西乡镇)及其所有父节点
    select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    003  001  深圳市
    007  003  宝安区
    008  007  西乡镇(所影响的行数为 4 行)
    */drop table tb
    drop function f_pid
      

  6.   

    create table tb(id varchar(3) , pid varchar(3) , name1 varchar(10) , name2 varchar(10))
    insert into tb values('1',  null , 's'  , '2') 
    insert into tb values('2',  '1'  , '22' , '22') 
    insert into tb values('3',  null , 'ss' , '12')  
    insert into tb values('4',  '2'  , 'ss2', '2')  
    insert into tb values('5',  '3'  , '1'  , 'w')  
    insert into tb values('6',  '4'  , '2'  , 'q1')  
    go--查询指定节点及其所有子节点的函数
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @id , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.id , @level
        from tb a , @t_Level b
        where a.pid = b.id and b.level = @level - 1
      end
      return
    end
    go--查询指定节点及其所有父节点的函数
    create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
    as
    begin
      insert into @t_level select @id
      select @id = pid from tb where id = @id and pid is not null
      while @@ROWCOUNT > 0
      begin
        insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
      end
      return
    end
    go--调用函数查询'1'
      select a.* from tb a , f_cid('1') b where a.id = b.id 
      union 
      select a.* from tb a , f_pid('1') b where a.id = b.id 
      order by a.id
    /*
    id   pid  name1      name2      
    ---- ---- ---------- ---------- 
    1    NULL s          2
    2    1    22         22
    4    2    ss2        2
    6    4    2          q1(所影响的行数为 4 行)
    */--调用函数查询'2'
      select a.* from tb a , f_cid('2') b where a.id = b.id 
      union 
      select a.* from tb a , f_pid('2') b where a.id = b.id 
      order by a.id
    /*
    id   pid  name1      name2      
    ---- ---- ---------- ---------- 
    1    NULL s          2
    2    1    22         22
    4    2    ss2        2
    6    4    2          q1(所影响的行数为 4 行)
    */--调用函数查询'3'
      select a.* from tb a , f_cid('3') b where a.id = b.id 
      union 
      select a.* from tb a , f_pid('3') b where a.id = b.id 
      order by a.id
    /*
    id   pid  name1      name2      
    ---- ---- ---------- ---------- 
    3    NULL ss         12
    5    3    1          w(所影响的行数为 2 行)
    */
    drop table tbdrop function f_pid , f_cid
      

  7.   

    爱新觉罗..回答正确;;
    只不过没想到  还要用函数.
    还以为能用一句sql语句就可以了..
    ok 结贴