ALL IN ONE : 利用存储过程实现BBS树形结构的存储及有回复email

80酷酷网    80kuku.com

  存储过程    BBS的树形结构一直是大家讨论的话题,以前我做都是利用命名规则来实现,这样的好处是表的冗余字段少,结构清楚,容易理解,但其局限性也很明显。感谢廖家远提供算法(实话说,当年算法就没有学好),我决定采用一下这种算法来实现bbs的树形结构。基本思路如下:
    bbs文章表中有这样几个字段:
    RootID :   根ID , 新发贴子及其所有子贴都相同。
    FatherID:  父ID , 父贴子ID
    Layer:     层数 , 贴子在树中的深度。
    OrderNum:  排序基数,关键所在,根据它来排序。

基本算法举例如下:

根16(拿个小的举例)
id    ordernum              Layer                
1     16                     0
2     16+16/2                1   回复第1贴
3     16+16/(2^2)            1   回复第1贴
4     16+16/2+16/(2^3)       2   回复第2贴
5     16+16/(2^2)+16/(2^4)   2   回复第3贴

然后,根据排序的结果是(加上回复的深度,就成了树状结构)
id      ordernum                  深度
1       16                          0
3       16+16/(2^2)                 1
5       16+16/(2^2)+16/(2^4)        2
2       16+16/2                     1
4       16+16/2+16/(2^3)            2

成了这样的树:
1
  3
     5
  2
     4

根据以上思路,我们设计表如下:

/*BBS文章表*/
if exists (select * from sysobjects where ID = object_id("BBS"))
   drop table BBS
go

create table BBS
             (
        ID        int primary key identity        not null ,
                RootID        int        default 0        not null ,  
        FatherID    int         default 0        not null ,
        Layer        tinyint        default 0        not null ,
                ForumID         int             default 0        not null ,
        UserID        int        default 0        not null ,
        Title        varchar(255)    default ""        not null ,
        Content        text        default ""             ,
        PostTime  

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: