日期:2014-05-17  浏览次数:20770 次

Oracle 评论排序!
现有三个表:版块表
 create table catalog 
(
catalogid Number(9),
names Varchar2(500)
)
主贴表
create table mainBbs2
(
bid Number(9),
catalogid Number(9),
titles Varchar2(500),
author Varchar2(50),
createDate Date
)
回帖表
create table reBbs 
(
rbid Number(9),
bid Number(9),
titles Varchar2(500),
author Varchar2(50),
createDate Date
)
insert into catalog values(se1_contract.nextval,'web开发');
insert into catalog values(se1_contract.nextval,'JS开发');

insert into mainBbs2 values (se1_contract.nextval,21,'JSP问题','橙子',sysdate);
insert into mainBbs2 values (se1_contract.nextval,21,'JSP问题','橙子',sysdate);

insert into mainBbs2 values (se1_contract.nextval,22,'JS问题','橙子',sysdate);
insert into mainBbs2 values (se1_contract.nextval,22,'JS问题','橙子',sysdate);
insert into mainBbs2 values (se1_contract.nextval,22,'JS问题','橙子',sysdate);


insert into rebbs values(se1_contract.nextval,33,'回复JSP问题','橙子',sysdate);
insert into rebbs values(se1_contract.nextval,33,'回复JS问题','橙子',sysdate);

问题:
1、查询出某版块下 回复数前10的帖子。
2、查询出某版块下 最新回复的前10个帖子。
要查询出结果的字段:
版块ID 版块名称,
catalogid 
names 
主贴:id 标题,创建时间,作者,
评论表:前十中 每个帖子的总回复数,最后回复时间
这样查:不行,很多不是分组字段
select c.catalogid,c.names,m.bid,m.titles,
m.author,m.createDate,count(r.bid) totalR,max(r.createDate) from rebbs r,mainBbs2 m,catalog c where r.bid=m.bid and m.catalogid=c.catalogid group by r.bid order by totalR;

这样查:还有很多需要的字段信息得不到
select r.bid,count(r.bid) totalR,max(r.createDate) from rebbs r,mainBbs2 m,catalog c where r.bid=m.bid and m.catalogid=c.catalogid group by r.bid order by totalR;
望高手指点啊!全部解决,可以追加分!


------解决方案--------------------
回复数前十的sql
select * from mainBbs2,reBbs,catalog where mainBbs2.bid =reBbs.bid and mainBbs2.catalogid = catalog.catalogid
and mainBbs2.bid in{

select bid from (
select bid,count(*) cc from reBbs group by bid where rowno<11 order by cc )--查出前十的帖子id
}

以此类推 最新回复前十

你可以下载本人写的工具 万能代码生成器 http://download.csdn.net/detail/lzq1205/4062483
------解决方案--------------------
方法一:
select A.*,B.* from (select c.catalogid,c.names,m.bid,m.titles,
m.author,r.createDate from rebbs r,mainBbs2 m,catalog c where r.bid=m.bid and m.catalogid=c.catalogid ) A,
(select top 10 r.bid,count(r.bid) totalR,max(r.createDate) as createDate from rebbs r,mainBbs2 m,catalog c where r.bid=m.bid and m.catalogid=c.catalogid group by r.bid having max(r.createDate) order by totalR) B
where B.bid = A.bid and A.createDate = B.createDate;

方法二:
select A.*,B.* from (select c.catalogid,c.names,m.bid,m.titles,
m.author,r.createDate from rebbs r,mainBbs2 m,catalog c where r.bid=m.bid and m.catalogid=c.catalogid ) A right join
(select top 10 r.bid,count(r.bid) totalR,max(r.createDate) as createDate from rebbs r,mainBbs2 m,catalog c where r.bid=m.bid and m.catalogid=c.catalogid group by r.bid having max(r.createDate) order by totalR) B
on B.bid = A.bid and A.createDate = B.createDate;
------解决方案--------------------
问题都没说清楚

如果某个版块中的帖子没有 10 个,或者没有回复的呢?