ITeye博客

db2还是蛮强的

jedy · 2005-12-08 03:06 · Markdown

开始看db2 cookbook,以前一直用MySQL,现在发现它的功能还是太少了。

一个树形表,

ID          PID         NAME
----------- ----------- ----------
          1           1 a
          2           2 b
          3           1 c
          4           1 d
          5           3 e
          6           5 f
要把同一枝一次取出来,利用db2中的递归,很容易就可以实现。

with tmp (id) as (
    select id from test where pid = 1 and id = pid
    union all
    select test.id from test,tmp where test.pid = tmp.id and test.id <> tmp.id
)
select * from test where id in (select id from tmp)

 

ID          PID         NAME
----------- ----------- ----------
          1           1 a
          3           1 c
          4           1 d
          5           3 e
          6           5 f

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。