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

求SQL语句:24小时评论排行榜(只要前3条数据)
表Subjects(评论表)
字段
sno(自动编号)
gno(商品编号:这个字段是记录商品的编号,就是说本条评论对应的是该商品)
putOut(评论的用户名)
poDate(评论时间)
content(评论内容)
comType(评论类型:条件语句中where comType = '评论' 就可以了)
poState(评论状态:条件语句中where poState = '通过' 就可以了)

表Goods(商品表)
字段
gno(商品编号:这个字段需与表Subjects的gno字段相关联)
title(商品名)

求24小时评论排行榜(只要前3条数据)
我的理解应该是24小时内评论数最多的商品依次排名,第一名,第二名,第三名。前台显示需要有商品名,用户名,评论时间,评论内容。

求7天评论排行榜(只要前3条数据)
我的理解应该是7天内评论数最多的商品依次排名,第一名,第二名,第三名。前台显示需要有商品名,用户名,评论时间,评论内容。

------解决方案--------------------
SQL code

--求24小时评论排行榜(只要前3条数据)
select top 3 
row_number() over(order by getdate()) '排名',
a.gno '商品编号',b.title '商品名',a.ct '评论数'
from 
(select gno,count(*) ct 
 from Subjects
 where comType='评论' and poState='通过'
 and datediff(hh,poDate,getdate())<=24
 group by gno
) a
inner join Goods b on a.gno=b.gno
order by a.ct desc


--求7天评论排行榜(只要前3条数据)
select top 3 
row_number() over(order by getdate()) '排名',
a.gno '商品编号',b.title '商品名',a.ct '评论数'
from
(select gno,count(*) ct 
 from Subjects
 where comType='评论' and poState='通过'
 and datediff(d,poDate,getdate())<=7
 group by gno
) a
inner join Goods b on a.gno=b.gno
order by a.ct desc