CatId CatName ParentId
2    产品专区  0
5    精美饰品  2
6    女性用品  2
7    精致礼品  2
8    日常用品  2
33    头饰   5
10    手表   5
11    挂件   5
14    彩妆   6
15    护肤   6
19    玩具   7
20    动漫   7
21    桌游   7
25    数码配件 8
26    手提包  8
27    皮夹   8需求:随便选哪个CatId都能找出他下面的所有子级。【要用一条sql语句来写】
难点在于CatId是2,该怎么找出他下面的所有子级。请高手帮忙写一下。

解决方案 »

  1.   

    遞歸?
    USE test
    GO
    -->生成表tbif object_id('tb') is not null 
    drop table tb
    Go
    Create table tb([CatId] smallint,[CatName] nvarchar(4),[ParentId] smallint)
    Insert into tb
    Select 2,N'产品专区',0
    Union all Select 5,N'精美饰品',2
    Union all Select 6,N'女性用品',2
    Union all Select 7,N'精致礼品',2
    Union all Select 8,N'日常用品',2
    Union all Select 33,N'头饰',5
    Union all Select 10,N'手表',5
    Union all Select 11,N'挂件',5
    Union all Select 14,N'彩妆',6
    Union all Select 15,N'护肤',6
    Union all Select 19,N'玩具',7
    Union all Select 20,N'动漫',7
    Union all Select 21,N'桌游',7
    Union all Select 25,N'数码配件',8
    Union all Select 26,N'手提包',8
    Union all Select 27,N'皮夹',8
    DECLARE @CatId smallintSET @CatId=2
    ;WITH t AS (
    SELECT 
    *
    FROM tb
    WHERE CatId=@CatId
    UNION ALL
    SELECT
    a.*
    FROM tb AS a
    INNER JOIN t AS b ON a.ParentId=b.CatId
    )SELECT CatName FROM t/*
    CatName
    -------
    产品专区
    精美饰品
    女性用品
    精致礼品
    日常用品
    数码配件
    手提包
    皮夹
    玩具
    动漫
    桌游
    彩妆
    护肤
    头饰
    手表
    挂件
    */
      

  2.   

    不知道你这个怎么用啊,报错:关键字 'WITH' 附近有语法错误。 ')' 附近有语法错误。
      

  3.   

    select * from Goods where CatId in(1楼求得的CatId)  该怎么写?
      

  4.   

    是我没说清楚,还有一个Goods表,里面有个CatId字段,最终是要取出Goods表的记录,1楼已经求得Category表的CatId,但是不是我要的结果,反正就是这样:select * from Goods where CatId in( 1楼求得的CatId )  该怎么写? 哎,表达不清了,不知道大家有没有看懂呢?
      

  5.   

    表Category               表Goods
    CatId CatName ParentId     GoodsId  CatId
    2    产品专区  0        1     33
    5    精美饰品  2        2     10
    6    女性用品  2        3     11
    7    精致礼品  2        4     15
    8    日常用品  2        5     19
    33    头饰   5        6      20
    10    手表   5        7      25
    11    挂件   5        8      26
    14    彩妆   6        9      27
    15    护肤   6
    19    玩具   7
    20    动漫   7
    21    桌游   7
    25    数码配件 8
    26    手提包  8
    27    皮夹   8需求:随便选哪个CatId都能找出他下面的所有子级。最终显示的结果应该是Goods的相关信息。
      

  6.   

    引用1楼的数据.
    2005(把一楼的改一下就行了):
    DECLARE @CatId smallint 
    SET @CatId=2 
     ;WITH t AS (SELECT *  FROM tb WHERE CatId=@CatId UNION ALL SELECT a.* FROM tb AS a  INNER JOIN t AS b ON a.ParentId=b.CatId)
    select * from Goods where 
    CatId in ( SELECT CatId FROM t ) and  CatId<>@CatId
    2000:declare @t table([CatId] smallint,[CatName] nvarchar(4),[ParentId] smallint,level int)
    declare @level int
    DECLARE @CatId smallint 
    SET @CatId=2
    set @level=0
    insert into @t 
    select CatId,CatName,ParentId,@level from tb where CatId=@CatId
     
    while @@rowcount>0
    begin
     set @level=@level+1
     insert into @t 
     select tb.CatId,tb.CatName,tb.ParentId,@level 
     from tb join  @t as t on t.CatId=tb.ParentId where t.level=@level-1
    end
    select * from Goods where 
    CatId in ( SELECT CatId FROM @t ) and  CatId<>@CatId