现在想实现指定一个父级id  获取子类所有信息,没有下级类别,就获取当前类别信息
要动态哦..Class 类别表(ClassID 类别ID    ClassName 类别名   ClassParentID父ID)
ClassID     ClassName    ClassParentID
1           娱乐资讯        0
2           体育在线     0
3           财经播报        0
4           电影            1
5           综艺            1
6           演出            1
7           首映礼          4Info 信息表
InfoID       InfoTitle       ClassID       
1            中国合伙人        7
2            致青春            7
3            雷神2             4
4            狼少年            4
5            天天向上          5
6            萧龙王上海演唱会  6

解决方案 »

  1.   

     with cte as
      (select * from class 
      union all
      select a.classid,a.classname,b.classparentid from class a,cte b where b.classid=a.classparentid)select a.classparentid, case when isnull(b.infotitle,'')='' then a.classname else b.infotitle end   from cte a left join info b on a.classid=b.classid 
      

  2.   


    DECLARE @ID INT
    SET @ID=1;WITH  info(InfoID,InfoTitle,ClassID) 
    AS
    (
    select 1,'中国合伙人',7 union all 
    select 2,'致青春',7 union all 
    select 3,'雷神2', 4 union all 
    select 4,'狼少年',4 union all 
    select 5,'天天向上',5 union all 
    select 6,'萧龙王上海演唱会',6
    ),
    Tree ( ClassID, ClassName, ClassParentID )
    AS (
    select   0 ,'影视' ,null   union all 
    select   1 ,'娱乐资讯' , 0 union all 
    select   2 ,'体育在线' ,0 union all 
    select   3 ,'财经播报' ,0 union all 
    select   4 ,'电影' ,1 union all 
    select   5 ,'综艺' , 1 union all 
    select   6 ,'演出' ,1 union all 
    select   7 ,'首映礼' ,4
    ),
    subTree 
    AS
    (
    SELECT * 
    FROM Tree 
    WHERE ClassID=@ID UNION ALL SELECT t.* 
    FROM subTree s 
    JOIN Tree t ON t.ClassParentID=s.ClassID
    )
    SELECT a.*,b.InfoTitle
    FROM  subTree a LEFT JOIN info b ON a.ClassID=b.ClassID--ClassID ClassName ClassParentID InfoTitle
    --1 娱乐资讯 0 NULL
    --4 电影 1 雷神2
    --4 电影 1 狼少年
    --5 综艺 1 天天向上
    --6 演出 1 萧龙王上海演唱会
    --7 首映礼 4 中国合伙人
    --7 首映礼 4 致青春
      

  3.   

    ;with class( ClassID, ClassName, ClassParentID ) as
    (
    select 1,'娱乐资讯',0
    union all select 2,'体育在线',0
    union all select 3,'财经播报',0
    union all select 4,'电影',1
    union all select 5,'综艺',1
    union all select 6,'演出',1
    union all select 7,'首映礼',4
    ),
    info(InfoID,InfoTitle,ClassID) AS
    (
    select 1,'中国合伙人',7
    union all select 2,'致青春',7
    union all select 3,'雷神',4
    union all select 4,'狼少年',4
    union all select 5,'天天向上',5
    union all select 6,'萧龙王上海演唱会',6
    )
    select a.*,stuff(ISNULL(','+d.ClassName,'')+ISNULL(','+c.ClassName,'')+ISNULL(','+b.ClassName,''),1,1,'') as result
    from info a
    left join class b on a.ClassID=b.ClassID
    left join class c on b.ClassParentID=c.ClassID
    left join class d on c.ClassParentID=d.ClassID/*
    InfoID InfoTitle ClassID result
    1 中国合伙人 7 娱乐资讯,电影,首映礼
    2 致青春 7 娱乐资讯,电影,首映礼
    3 雷神 4 娱乐资讯,电影
    4 狼少年 4 娱乐资讯,电影
    5 天天向上 5 娱乐资讯,综艺
    6 萧龙王上海演唱会 6 娱乐资讯,演出
    */
      

  4.   

    我C# 只要调用sql
    那么大段是sql语句么
      

  5.   

    表:
    info(InfoID,InfoTitle,ClassID)
    Tree ( ClassID, ClassName, ClassParentID ) DECLARE @ID INT
    SET @ID=1
     
    ;WITH subTree 
    AS
    (
        SELECT * 
        FROM Tree 
        WHERE ClassID=@ID
     
        UNION ALL
     
        SELECT t.* 
        FROM subTree s 
        JOIN Tree t ON t.ClassParentID=s.ClassID
    )
     
     
    SELECT a.*,b.InfoTitle
    FROM  subTree a LEFT JOIN info b ON a.ClassID=b.ClassID
     
    --ClassID    ClassName    ClassParentID    InfoTitle
    --1    娱乐资讯    0    NULL
    --4    电影    1    雷神2
    --4    电影    1    狼少年
    --5    综艺    1    天天向上
    --6    演出    1    萧龙王上海演唱会
    --7    首映礼    4    中国合伙人
    --7    首映礼    4    致青春