日期:2014-05-18  浏览次数:20455 次

如何统计数目
今天看到题问如何统计发贴数和回帖数?库是bbsDB发贴表bbsTopic回帖表bbsReply
表如下
bbsUser
UID           Uname         Upassword     Uemail           Usex   Uclass             Uremark       UregDate  
bbsTopic
TID         TsID         TuID         TreplyCount     Tface     Ttopic             Tcontents    
bbsReply
RID                   RtID                 RsID                 RuID                 Rface               Rcontents
如何统计Uname= 'xx '发贴数和回帖数?
还有我是新手不知道看什么书好希望大家推荐下,谢谢了   !


------解决方案--------------------
select t1.uname,t1.发贴数,t2.回帖数 from
(select uname,count(*) 发贴数 from bbsUser a,bbsTopic b where a.uid = b.tuid and b.uname = 'xx ') t1,
(select uname,count(*) 回帖数 from bbsUser a,bbsReply b where a.uid = b.tuid and b.uname = 'xx ') t2
where t1.uname = t2.uname
------解决方案--------------------

select st=(select count(*) from bbstopic a left join bbsuser b on b.uid=a.tuid where b.uname= 'XX '),
sr=(select count(*) from bbsReply a left join bbsuser b on b.uid=a.RuID where b.uname= 'XX ')
------解决方案--------------------
--建議用count(1)或count(主鍵),要比count(*)快
select t1.uname,t1.发贴数,t2.回帖数 from
(select uname,count(1) 发贴数 from bbsUser a,bbsTopic b where a.uid = b.tuid and b.uname = 'xx ') t1,
(select uname,count(1) 回帖数 from bbsUser a,bbsReply b where a.uid = b.ruid and b.uname = 'xx ') t2
where t1.uname = t2.uname