编号    级别      名称       深度1 0 新闻    1
2 0 财经    1
3 1 军事    2
4 2 股票    3
5 0 房产    1
6 1 情感    2
7 1 国内    2
8 1 国际    2
14 2 邮政电信    2
15 2 环保    2
16 5 IT企业    2
17 5 生活用品    2
18 5 游戏与网络  2
19 2 能源工业    2
20 2 化学工业    2怎么把父级下一级的内容各查出2条  下面的是把全部都查出来的!编号    级别      名称       深度3 1 军事     2
6 1 情感     2
7 1 国内     2
8 1 国际     2
14 2 邮政电信     2
15 2 环保     2
16 5 IT企业     2
17 5 生活用品     2
18 5 游戏与网络   2
19 2 能源工业     2
20 2 化学工业     2

解决方案 »

  1.   


    /*
    标题:SQL SERVER 2000中查询指定节点及其所有父节点的函数(表格形式显示)
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 
    时间: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
      

  2.   

    http://blog.csdn.net/htl258/archive/2009/04/03/4014748.aspx
      

  3.   

    呵呵,很强大,我想显示在首页导航那,类似http://www.qq.com/的,要怎和处理呢
      

  4.   

    父级下一级的内容各查出2条,第三级的不需要呢,你刚才发的那个没有实现出来呢
    ID     parentID  NAME   deep
    3 1 军事 2
    6 1 情感 2
    7 1 国内 2
    8 1 国际 2
    14 2 邮政电信 2
    15 2 环保 2
    16 5 IT企业 2
    17 5 生活用品 2
    18 5 游戏与网络2
    19 2 能源工业 2
    20 2 化学工业 2我只要parentID  相同的各2条查询出来,怎么写呢
      

  5.   

    “怎么把父级下一级的内容各查出2条 下面的是把全部都查出来的!”
    “我只要parentID     相同的各2条查询出来,怎么写呢”不懂!把你最终想要的效果贴出来,让大家看看吗!
      

  6.   

    --> 测试数据: [tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb] (ID int,parentID int,NAME varchar(11),deep int)
    insert into [tb]
    select 3,1,'军事',2 union all
    select 6,1,'情感',2 union all
    select 7,1,'国内',2 union all
    select 8,1,'国际',2 union all
    select 14,2,'邮政电信',2 union all
    select 15,2,'环保',2 union all
    select 16,5,'IT企业',2 union all
    select 17,5,'生活用品',2 union all
    select 18,5,'游戏与网络2',null union all
    select 19,2,'能源工业',2 union all
    select 20,2,'化学工业',2select * from tb a
    where (select count(*) from tb where parentid=a.parentid and id<=a.id)<3
      

  7.   


    --结果:
    ID          parentID    NAME        deep
    ----------- ----------- ----------- -----------
    3           1           军事          2
    6           1           情感          2
    14          2           邮政电信        2
    15          2           环保          2
    16          5           IT企业        2
    17          5           生活用品        2