表Subjects(评论表)
字段
sno(自动编号)
gno(商品编号:这个字段是记录商品的编号,就是说本条评论对应的是该商品)
putOut(评论的用户名)
poDate(评论时间)
content(评论内容)
comType(评论类型:条件语句中where comType = '评论' 就可以了)
poState(评论状态:条件语句中where poState = '通过' 就可以了)表Goods(商品表)
字段
gno(商品编号:这个字段需与表Subjects的gno字段相关联)
title(商品名)求24小时评论排行榜(只要前3条数据)
我的理解应该是24小时内评论数最多的商品依次排名,第一名,第二名,第三名。前台显示需要有商品名,用户名,评论时间,评论内容。求7天评论排行榜(只要前3条数据)
我的理解应该是7天内评论数最多的商品依次排名,第一名,第二名,第三名。前台显示需要有商品名,用户名,评论时间,评论内容。

解决方案 »

  1.   

    不能保证我写得对,只是希望你有个思路
    select top 3 title,putOut,poDate,content from Subjects,Goods where Goods.gno = Subjects.gno and comType = '评论' and poState = '通过' and datediff(hour,poDate,getdate())<=24select top 3 title,putOut,poDate,content from Subjects,Goods where Goods.gno = Subjects.gno and comType = '评论' and poState = '通过' and datediff(day,poDate,getdate())<=7
      

  2.   


    --求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