如何让新闻发表评论?

时间:2018-07-27 21:13:09

标签: sql

我想获得包含评论的新闻,按评论总数排序

我尝试使用此SQL代码

SELECT COUNT( comment ) , N . * , C . * 
FROM news N, comment C
WHERE N.ID = C.ID_NEWS

但是问题是它只返回第一个包含所有评论的新闻

有什么解决办法吗?

1 个答案:

答案 0 :(得分:1)

我认为您想要这样的查询:

SELECT N.ID as comments_order, 
       count( C.comment ) as total_comments
  FROM news N INNER JOIN comment C
    ON (  N.ID = C.ID_NEWS )
 GROUP BY N.ID
 ORDER BY total_comments;