【发布时间】:2020-08-10 22:49:09
【问题描述】:
我有两张表,Post & Comment。帖子表包含有关帖子的信息,评论表包含有关每个帖子的 cmets 数据。下面是结构,
Post Table
post_id (Primary Key), post_title, post_description
Comment Table
comment_id (Primary Key), comment_text, fk_post_id (Foreign Key)
我需要从帖子表中获取 post_id、post_title 以及评论表中每个帖子的 cmets 总数(使用聚合函数计数),并希望数据像这样显示。
Post_ID Post_Title Total_Comments
1 ABC 4
2 XYZ 5
3 123 3
将通过计算特定 post_id 的所有行从 cmets 表中获取总 cmets。
我设法编写了一个内部连接查询,但不知道如何以及在何处放置聚合函数“count”以获取所有 cmets 的总数。以下是我的查询,
select post.post_id, post.post_title, comment.comment_id from post INNER JOIN comment on
post.post_id = comment.fk_post_id ;
谢谢。
【问题讨论】:
标签: jquery mysql sql join inner-join