Heim  >  Artikel  >  Backend-Entwicklung  >  用group by 不符合条件他不返回数据,该怎么解决

用group by 不符合条件他不返回数据,该怎么解决

WBOY
WBOYOriginal
2016-06-13 11:43:38700Durchsuche

用group by 不符合条件他不返回数据
如:posts帖子表
create table posts(
posts_id,
forumid, (论坛ID)
posts_title,
posts_postTime,
)

comments评论表
create table comments( 
comments_id,
postsid,
comments_content,
comments_postTime,
 )

我要的结果是:读出论坛ID为1的所有帖子,并统计每个帖子的评论总数.
我用count(comments_id) AS commentsNum 统计评论,但帖子在评论表没有评论,group by 就忽略了
posts_id  forumid   commentsNum
 1           1         4
 2           1         5
 3           1         4
 4           1         0
我要当帖子在评论表里没有评论的时候,返回0,或者返回Null也行啊,怎么弄

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn