shabir ullah shabir ullah - 3 months ago 7
MySQL Question

How to calculate sum of comments through specific id

I have two table one is

blog
which contains title and description while the second one is
comment
which shows all comments related to specific
blog_id
.

blog_id
is a primary key for the
blog
table while used as foreign key in the
comment
table.

Answer

Suppose your blog id is 1

SELECT comments.blog_id AS blog_id, count(comments.blog_id) AS total_comment from comments where comments.blog_id = 1 group by comments.blog_id
Comments