如: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也行啊,怎么弄
select a.posts_id,a.forumid,count(b.comments_id) as commentsNum from posts a left join comments b on a.posts_id=b.postsidgroup by b.postsid having a.forumid=1
解决了,谢版主,有想到Having 却没去用 = =
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号