表不太清楚,是这样的!!1表 Users: 
UsersID  UsersName 1       franky 
2      fengwe 2表 Club: 
ClubID   ClubName 1        fengClub 
2        frankClub 3表 ClubBBS: 注:版块 
ClubBBSID    ClubID 
1              2 
2              1 4表 Bbs: 注:版块内的帖子 
BbsID   BbsTitle             UsersID 1       franky is man      2 
2       I am a body           1 5表 BbsRep:  注:回复主题帖子 
BbsRepID   BbsID   BbsRepTitle 
1          1       Good! 
2          1       Very Good! 
3          2       No,man! 

解决方案 »

  1.   

    最好说说各表的关系..
    --> 测试数据: #Users
    if object_id('tempdb.dbo.#Users') is not null drop table #Users
    create table #Users (UsersID int,UsersName varchar(6))
    insert into #Users
    select 1,'franky' union all
    select 2,'fengwe'
    --> 测试数据: #Club
    if object_id('tempdb.dbo.#Club') is not null drop table #Club
    create table #Club (ClubID int,ClubName varchar(9))
    insert into #Club
    select 1,'fengClub' union all
    select 2,'frankClub'
    --> 测试数据: #ClubBBS
    if object_id('tempdb.dbo.#ClubBBS') is not null drop table #ClubBBS
    create table #ClubBBS (ClubBBSID int,ClubID int)
    insert into #ClubBBS
    select 1,2 union all
    select 2,1
    --> 测试数据: #Bbs
    if object_id('tempdb.dbo.#Bbs') is not null drop table #Bbs
    create table #Bbs (BbsID int,BbsTitle varchar(11),UsersID int)
    insert into #Bbs
    select 1,'frankyisman',2 union all
    select 2,'Iamabody',1
    --> 测试数据: #BbsRep
    if object_id('tempdb.dbo.#BbsRep') is not null drop table #BbsRep
    create table #BbsRep (BbsRepID int,BbsID int,BbsRepTitle varchar(10))
    insert into #BbsRep
    select 1,1,'Good!' union all
    select 2,1,'VeryGood!' union all
    select 3,2,'Noman!'select u.usersname,c.clubname,count(*) cnt
    from #Club c
     join #ClubBBS cb 
       on c.ClubID=cb.ClubID
     join #Bbs bbs
       on cb.ClubBBSID=bbs.BbsID
     join #BbsRep br
       on bbs.BbsID=br.BbsID
     join #users u
       on bbs.usersID=u.UsersID
    group by u.usersname,c.clubname
    order by count(*) desc
    /*
    usersname clubname  cnt
    --------- --------- -----------
    fengwe    frankClub 2
    franky    fengClub  1(2 行受影响)
    */
      

  2.   

    这个是关系图,帮我看一下谢谢!我想以 
    BbsTitle,UsersName,ClubName,统计结果,     以统计来排序
      

  3.   

    --> 测试数据: #Users
    if object_id('tempdb.dbo.#Users') is not null drop table #Users
    create table #Users (UsersID int,UsersName varchar(6))
    insert into #Users
    select 1,'franky' union all
    select 2,'fengwe'
    --> 测试数据: #Club
    if object_id('tempdb.dbo.#Club') is not null drop table #Club
    create table #Club (ClubID int,ClubName varchar(9))
    insert into #Club
    select 1,'fengClub' union all
    select 2,'frankClub'
    --> 测试数据: #ClubBBS
    if object_id('tempdb.dbo.#ClubBBS') is not null drop table #ClubBBS
    create table #ClubBBS (ClubBBSID int,ClubID int)
    insert into #ClubBBS
    select 1,2 union all
    select 2,1
    --> 测试数据: #Bbs
    if object_id('tempdb.dbo.#Bbs') is not null drop table #Bbs
    create table #Bbs (BbsID int,ClubBBSID int,BbsTitle varchar(11),UsersID int)
    insert into #Bbs
    select 1,2,'frankyisman',2 union all
    select 2,1,'Iamabody',1
    --> 测试数据: #BbsRep
    if object_id('tempdb.dbo.#BbsRep') is not null drop table #BbsRep
    create table #BbsRep (BbsRepID int,BbsID int,BbsRepTitle varchar(10))
    insert into #BbsRep
    select 1,1,'Good!' union all
    select 2,1,'VeryGood!' union all
    select 3,2,'Noman!'select bbs.bbsTitle,u.UsersName,c.ClubName,count(*) cnt
    from #Users u
      join #Bbs bbs
         on u.UsersID=bbs.UsersID
      join #ClubBBS cb
         on bbs.ClubBBSID=cb.ClubBBSID
      join #BBSRep br
         on bbs.bbsID=br.BBSID
      join #Club c
         on cb.ClubID=c.ClubID
    group by bbs.bbsTitle,u.UsersName,c.ClubName
    order by count(*) desc
    /*
    bbsTitle    UsersName ClubName  cnt
    ----------- --------- --------- -----------
    frankyisman fengwe    fengClub  2
    Iamabody    franky    frankClub 1(2 行受影响)*/
      

  4.   

    基本的问题都解决了!!我现在正在分析应该把 主题帖子 没有回复的,显示为0!!!!bbsTitle    UsersName ClubName  cnt
    ----------- --------- --------- -----------
    frankyisman fengwe    fengClub  2
    Iamabody    franky    frankClub 1
    我们在一起    franky    fengClub  0这样显示!!!??????
      

  5.   

    --> 测试数据: #Users
    if object_id('tempdb.dbo.#Users') is not null drop table #Users
    create table #Users (UsersID int,UsersName varchar(6))
    insert into #Users
    select 1,'franky' union all
    select 2,'fengwe'
    --> 测试数据: #Club
    if object_id('tempdb.dbo.#Club') is not null drop table #Club
    create table #Club (ClubID int,ClubName varchar(9))
    insert into #Club
    select 1,'fengClub' union all
    select 2,'frankClub'
    --> 测试数据: #ClubBBS
    if object_id('tempdb.dbo.#ClubBBS') is not null drop table #ClubBBS
    create table #ClubBBS (ClubBBSID int,ClubID int)
    insert into #ClubBBS
    select 1,2 union all
    select 2,1
    --> 测试数据: #Bbs
    if object_id('tempdb.dbo.#Bbs') is not null drop table #Bbs
    create table #Bbs (BbsID int,ClubBBSID int,BbsTitle nvarchar(11),UsersID int)
    insert into #Bbs
    select 1,2,'frankyisman',2 union all
    select 2,1,'Iamabody',1 union all
    select 3,1,N'这个没人回复',1
    --> 测试数据: #BbsRep
    if object_id('tempdb.dbo.#BbsRep') is not null drop table #BbsRep
    create table #BbsRep (BbsRepID int,BbsID int,BbsRepTitle varchar(10))
    insert into #BbsRep
    select 1,1,'Good!' union all
    select 2,1,'VeryGood!' union all
    select 3,2,'Noman!'select bbs.bbsTitle,u.UsersName,c.ClubName,isnull(count(br.bbsid),0) cnt
    from #Bbs bbs
      left join #Users u
         on u.UsersID=bbs.UsersID
      left join #ClubBBS cb
         on bbs.ClubBBSID=cb.ClubBBSID
      left join #BBSRep br
         on bbs.bbsID=br.BBSID
      left join #Club c
         on cb.ClubID=c.ClubID
    group by bbs.bbsTitle,u.UsersName,c.ClubName
    order by count(*) desc
    /*
    bbsTitle    UsersName ClubName  cnt
    ----------- --------- --------- -----------
    frankyisman fengwe    fengClub  2
    Iamabody    franky    frankClub 1
    这个没人回复      franky    frankClub 0
    */
      

  6.   

    我改用left join试了一下!
    select bbs.BbsID,bbs.bbsTitle,u.UsersName,c.ClubName,count(*) cnt
    from Users u
      left join Bbs bbs
         on u.UsersID=bbs.UsersID
      left join ClubBBS cb
         on bbs.ClubBBSID=cb.ClubBBSID
      left join BBSRep br
         on bbs.bbsID=br.BBSID
      left join Club c
         on cb.ClubID=c.ClubID
    group by bbs.BbsID,bbs.bbsTitle,u.UsersName,c.ClubName
    order by count(*) desc
    结果却是这样:
    bbsTitle    UsersName ClubName  cnt 
    ----------- --------- --------- ----------- 
    frankyisman fengwe    fengClub  2 
    Iamabody    franky    frankClub 1 
    我们在一起    franky    fengClub  1这是为什么呢?
     
      

  7.   

    select bbs.BbsID,bbs.bbsTitle,u.UsersName,c.ClubName,count(br.BbsID) cnt
    from Users u
      left join Bbs bbs
         on u.UsersID=bbs.UsersID
      left join ClubBBS cb
         on bbs.ClubBBSID=cb.ClubBBSID
      left join BBSRep br
         on bbs.bbsID=br.BBSID
      left join Club c
         on cb.ClubID=c.ClubID
    group by bbs.BbsID,bbs.bbsTitle,u.UsersName,c.ClubName
    order by count(br.BbsID) desc好了,弄好了!!