join + count + group + order 

解决方案 »

  1.   

    select u.UsersID,u.UsersLogo,u.UsersName,'ent'=isnull(e.cnt,0),'bb'=isnull(b.bnt,0),'cc'=isnull(cc.cnnt,0),'css'=isnull(css.csnt,0)
    from Users u
    left join Bbs bbs on u.UsersID=bbs.UsersID
    left join ColumnNews cn on u.UsersID=cn.UsersID
    left join Comments cs on u.UsersID=cs.UsersID
    left join (select BbsID,'cnt'=count(BbsID) from BbsRep group by BbsID)e  on e.BbsID=bbs.BbsID
    left join (select UsersID,'bnt'=count(BbsID) from Bbs group by UsersID)b  on b.UsersID=bbs.UsersID
    left join (select UsersID,'cnnt'=count(UsersID) from ColumnNews group by UsersID)cc  on cc.UsersID=u.UsersID
    left join (select UsersID,'csnt'=count(UsersID) from Comments group by UsersID)css  on css.UsersID=u.UsersID我写的语句,显示不正确!!!帮我看一下!!谢谢!
      

  2.   

    select a.UsersID ,a.UsersName
    ,count(distinct b.BbsID) as CountBbs
    ,count(c.BbsRepID) as CountBbsRep
    ,count(d.ColumnNewsID) as CountColumnNews
    ,count(e.CommentsID) as CountComments
    ,count(f.ImagesInfoID) as CountImagesInfo 
    ,count(distinct b.BbsID)+count(c.BbsRepID)+count(d.ColumnNewsID)+count(e.CommentsID)+count(f.ImagesInfoID) as Total 
    from Users  a
    left join Bbs b on b.UsersID =a.UsersID 
    left join BbsRep c on c.BbsID =b.BbsID 
    left join ColumnNews d on d.UsersID =a.UsersID 
    left join Comments e on e.UsersID =a.UsersID 
    left join ImagesInfo f on f.UsersID =a.UsersID 
    group by a.UsersID ,a.UsersName
      

  3.   

    没说明怎么排序,所以没写order by 部分
      

  4.   

    解决了!!!
    谢谢!!!学习!!!
    请问有没有什么好点儿书或是电子文档,学习SQL  ???
    我知道我的数据库操作很菜!!想学习一下!请指教!
      

  5.   

    -->生成测试数据
     
    declare @Users table([UsersID] int,[UsersName] nvarchar(6))
    Insert @Users
    select 1,N'franky' union all
    select 2,N'xiang' union all
    select 3,N'feng'
    --Select * from @Usersdeclare @Bbs table([BbsID] int,[UsersID] int)
    Insert @Bbs
    select 1,2 union all
    select 2,1
    --Select * from @Bbsdeclare @BbsRep table([BbsRepID] int,[BbsID] int)
    Insert @BbsRep
    select 1,1 union all
    select 2,1 union all
    select 3,2
    --Select * from @BbsRepdeclare @ColumnNews table([ColumnNewsID] int,[UsersID] int)
    Insert @ColumnNews
    select 1,2 union all
    select 2,2 union all
    select 3,1
    --Select * from @ColumnNewsdeclare @Comments table([CommentsID] int,[UsersID] int)
    Insert @Comments
    select 1,1 union all
    select 2,1
    --Select * from @Comments
    declare @ImagesInfo table([ImagesInfoID] int,[UsersID] int)
    Insert @ImagesInfo
    select 1,2 union all
    select 2,2 union all
    select 3,1
    --Select * from @ImagesInfo--UsersID ¦ UsersName ¦ CountBbs ¦ CountBbsRep ¦ CountColumnNews ¦ CountComments ¦ CountImagesInfo ¦ Total select u.UsersID,u.UsersName,
    'ent' = (select count(1) from @Bbs where UsersID = u.UsersID),
    'bb'  = (select count(1) from @BbsRep r left join @Bbs b on b.BbsID =r.BbsID where UsersID = u.UsersID),
    'cc'  = (select count(1) from @ColumnNews where UsersID = u.UsersID),
    'css' = (select count(1) from @Comments where UsersID = u.UsersID),
    'image' = (select count(1) from @ImagesInfo where UsersID = u.UsersID)from @Users u
    /*
    UsersID     UsersName ent         bb          cc          css         image
    ----------- --------- ----------- ----------- ----------- ----------- -----------
    1           franky    1           1           1           2           1
    2           xiang     1           2           2           0           2
    3           feng      0           0           0           0           0
    */
      

  6.   

    到目前为止,我认为sql server2000中文版的联机帮助是最好的电子文档
      

  7.   

    UsersID ¦ UsersName ¦ CountBbs ¦ CountBbsRep ¦ CountColumnNews ¦ CountComments ¦ CountImagesInfo ¦ Total 
    ---------------------------------------------------------------------------------------------
    select *,total=CountBbs+CountBbsRep+CountColumnNews+CountComments+CountImagesInfo 
    from (
          select 
             usersid     = u.usersid,
             usersname   = u.usersname,
             countbbs    = isnull((select count(*) from bbs where usersid = u.usersid),0),
             countbbsrep = isnull((select count(*) from bbs a,bbsrep b
                                   where a.bbsid = b.bbsid and a.usersid = u.usersid),0),
             countcolumnnews = isnull((select count(*) from ColumnNews
                                       where usersid = u.usersid),0)
             countComments = isnull((select count(*) from comments 
                                     where usersid = u.usersid),0)
             countImagesInfo = isnull((select count(*) from ImagesInfo
                                       where usersid = u.usersid),0)
     
          ) a