当我们的文章表中没有对于文章的评论数字段时,我们该这么写sql语句来显示出评论最多的文章呢?
下面本站给大家收集了几种方法,仅供参考:
1.
select documents.*,(select count(0) from comments where comments.documentid=documents.documentid) as commentcount
from documents order by commentcount desc
documents为文章表;comments为评论表
2.A表是新闻表 B表是新闻评论表 B表中以articleid与A表中的ID相关联
select a.id,count(b.id) as c from news as a inner join newsComm as b on a.id = b.articleid GROUP BY a.id order by c desc
3.
Select * From A Where ID in(Select max(count(articleid) From B)) order by ID Desc
当然最佳的方法,是在设计数据库时给文章表加一个记录评论数的字段,这样就很发表对评论进行查询了。