sql语句进行排序

lgwinym 2012-05-02 04:08:38
数据库为MYSQL


table a :content_id(内容ID)
table b: content_id(内容),comment_id(评论id)
table c: prasieID(赞ID),comment_id(评论id)

得到某条内容的评论列表 并且根据赞的个数进行排列
...全文
154 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyj0913 2012-05-03
  • 打赏
  • 举报
回复
select b.comment_id,count(*)as seq from b inner join c on b.comment_id=c.comment_id where b.content_id="值" group by b.comment_id order by seq desc

如果数据库不做优化的话,可以先把 where b.content_id="值" 条件一个子查询然后再 inner join c 表
  • 打赏
  • 举报
回复
select a.*,t.content_id(内容),t.total from a full join(
select b.content_id,count(c.prasieID) as total from b
full join c on b.comment_id=c.comment_id group by b.content_id)t
on a.content_id=t.content_id
order by total
迪迦凹凸曼 2012-05-02
  • 打赏
  • 举报
回复

select comment_id,cnt from (
select b.comment_id,sum(prasieID) as cnt from b inner join c on b.comment_id=c.comment_id
group by c.comment_id
where b.content_id=N'xxxxxx')
order by cnt desc

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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