首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一遇到递归就头大, 这个如何效率解决

2012-08-01 
一遇到递归就头大, 这个怎么效率解决?表ASQL codeidnamepid3c14d15e26f47g38h69i710j711k512l913m1014n815

一遇到递归就头大, 这个怎么效率解决?
表A

SQL code
id    name    pid3    c    14    d    15    e    26    f    47    g    38    h    69    i    710    j    711    k    512    l    913    m    1014    n    815    o    14

表B只有id列,如何能效率得到root列, 如下
SQL code
id    root4    17    19    111    212    115    1


[解决办法]
SQL code
;with t(id,pid,topid)as(select id,pid,pid     from A     where not exists (select 1 from A A1 where A.pid=A1.id)union allselect A.id,A.pid,t.topid    from A,t    where A.pid=t.id)select B.id,t.topid [root] from B,t where B.id=t.id; 

热点排行
Bad Request.