请教个SQL语句

cwbok 2013-04-11 10:29:12
ID GoodsID CommentClass
2 109 5
3 27 5
4 27 3
5 27 3

goodsid 是产品ID COMMENTCLASS 是评论级数


现在要按commentclass 数字相加最多的产品来排序,请问要怎么写???
...全文
174 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
aben402 2013-04-11
  • 打赏
  • 举报
回复
SELECT goodsid,SUM(commentclass) AS grade FROM goods GROUP BY goodsid ORDER BY grade DESC;
lhprince1 2013-04-11
  • 打赏
  • 举报
回复
像上面那么写,查询每条记录的时候是不是重新生成sortedgoodscomments?
lhprince1 2013-04-11
  • 打赏
  • 举报
回复

select goods.goodsid,goods.goodsname,sortedgoodscomments.allcomments from goods,
(select goodsid,SUM(commentclass) as allcomments from goodscomments group by goodsid) as sortedgoodscomments 
where goods.goodsid=sortedgoodscomments.goodsid order by sortedgoodscomments.allcomments
  • 打赏
  • 举报
回复
你要根据评论排序产品,不跨表肯定不行的 你产品表和评论本来就是分开2个表的
cwbok 2013-04-11
  • 打赏
  • 举报
回复
能把 GOODSID 索引一下 统计吗。 最好不要跨表,产品表的记录好多啊。
挨踢直男 2013-04-11
  • 打赏
  • 举报
回复
select * ,(
  select sum( commentclass ) 
     from 你的这张表名 as b 
     where a.产品的id = b.goodsid 
) as sortcol 
  from goods as a order by sortcol desc
试试看
fengyun817 2013-04-11
  • 打赏
  • 举报
回复
WITH t1
     AS (SELECT goodsid,
                Sum(CommentClass) num
         FROM   table_name
         GROUP  BY goodsid)
SELECT    a.ID,
          a.GoodsID,
          a.CommentClass
FROM      table_name a
LEFT JOIN t1 b ON a.goodsid = b.goodsid
ORDER     BY b.num desc 

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧