几种解决方法收集

    1. if object_id('[tb]'is not null drop table [tb]
    2. go
    3. create table [tb]([id] int,[yid] varchar(2),[t1] varchar(3),[t2] varchar(3))
    4. insert [tb]
    5. select 1,null,'s','2' union all
    6. select 2,'1','22','22s' union all
    7. select 3,null,'ss','12' union all
    8. select 4,'2','ss2','2' union all
    9. select 5,'3','1','w' union all
    10. select 6,'4','2','q1'
    11. --select * from [tb]
    12. create function fn_haha(@id int)
    13. returns @tb table([id] int,[yid] varchar(2),[t1] varchar(3),[t2] varchar(3))
    14. as
    15. begin
    16.     insert @tb select * from tb where id=@id
    17.     while @@rowcount>0
    18.         insert @tb 
    19.         select a.* 
    20.         from (select * from tb where id not in(select id from @tb)) a
    21.         join @tb b on a.id=b.yid or a.yid=b.id
    22.     return
    23. end
    24. go
    25. select * from dbo.fn_haha(2) order by id
    26. /*
    27. id          yid  t1   t2
    28. ----------- ---- ---- ----
    29. 1           NULL s    2
    30. 2           1    22   22s
    31. 4           2    ss2  2
    32. 6           4    2    q1
    33. (4 行受影响)
    34. */
    35. select * from dbo.fn_haha(3) order by id
    36. /*
    37. id          yid  t1   t2
    38. ----------- ---- ---- ----
    39. 3           NULL ss   12
    40. 5           3    1    w
    41. (2 行受影响)
    42. */
    SQL code
  1. create table tb(id varchar(3) , pid varchar(3) , name1 varchar(10) , name2 varchar(10))
  2. insert into tb values('1',  null , 's'  , '2') 
  3. insert into tb values('2',  '1'  , '22' , '22') 
  4. insert into tb values('3',  null , 'ss' , '12')  
  5. insert into tb values('4',  '2'  , 'ss2', '2')  
  6. insert into tb values('5',  '3'  , '1'  , 'w')  
  7. insert into tb values('6',  '4'  , '2'  , 'q1')  
  8. go
  9. --查询指定节点及其所有子节点的函数
  10. create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
  11. as
  12. begin
  13.   declare @level int
  14.   set @level = 1
  15.   insert into @t_level select @id , @level
  16.   while @@ROWCOUNT > 0
  17.   begin
  18.     set @level = @level + 1
  19.     insert into @t_level select a.id , @level
  20.     from tb a , @t_Level b
  21.     where a.pid = b.id and b.level = @level - 1
  22.   end
  23.   return
  24. end
  25. go
  26. --查询指定节点及其所有父节点的函数
  27. create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
  28. as
  29. begin
  30.   insert into @t_level select @id
  31.   select @id = pid from tb where id = @id and pid is not null
  32.   while @@ROWCOUNT > 0
  33.   begin
  34.     insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
  35.   end
  36.   return
  37. end
  38. go
  39. --调用函数查询'1'
  40.   select a.* from tb a , f_cid('1') b where a.id = b.id 
  41.   union 
  42.   select a.* from tb a , f_pid('1') b where a.id = b.id 
  43.   order by a.id
  44. /*
  45. id   pid  name1      name2      
  46. ---- ---- ---------- ---------- 
  47. 1    NULL s          2
  48. 2    1    22         22
  49. 4    2    ss2        2
  50. 6    4    2          q1
  51. (所影响的行数为 4 行)
  52. */
  53. --调用函数查询'2'
  54.   select a.* from tb a , f_cid('2') b where a.id = b.id 
  55.   union 
  56.   select a.* from tb a , f_pid('2') b where a.id = b.id 
  57.   order by a.id
  58. /*
  59. id   pid  name1      name2      
  60. ---- ---- ---------- ---------- 
  61. 1    NULL s          2
  62. 2    1    22         22
  63. 4    2    ss2        2
  64. 6    4    2          q1
  65. (所影响的行数为 4 行)
  66. */
  67. --调用函数查询'3'
  68.   select a.* from tb a , f_cid('3') b where a.id = b.id 
  69.   union 
  70.   select a.* from tb a , f_pid('3') b where a.id = b.id 
  71.   order by a.id
  72. /*
  73. id   pid  name1      name2      
  74. ---- ---- ---------- ---------- 
  75. 3    NULL ss         12
  76. 5    3    1          w
  77. (所影响的行数为 2 行)
  78. */
  79. drop table tb
  80. drop function f_pid , f_cid

 问题描述:

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=2[或1或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
-----------------

sql  解决方法:  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值