Suppose I have three tables-
blogs,
comments,
replies
In the blogs
table, I have one column named blog_id
, and this id is also a primary key of the blogs
table.
In the comments
table, I have comment_id
as the primary key, and comm_blog_id
as a foreign key with blog_id
.
In the replies
table, I have reply_id
as the primary key, reply_blog_id
as a foreign key with blog_id
, and reply_comm_id
as a foreign key with comment_id
.
Like this-
blog1
comment1
reply1
blog2
comment1
reply1
blog3
comment1
comment2
reply1
comment3
blog4
comment1
reply1
reply2
reply3
comment2
comment3
Now, if I want to calculate the total numbers count
of comments and replies
combined in blog1
has to be 2
in blog2
have to be 2
in blog3
to be 4
, and in blog4
was have to be 6
.
The query I have written as follows:
SELECT
blogs.blog_id = '1',
(
(SELECT COUNT(*) FROM comments WHERE comments.comm_blog_id = blogs.blog_id) +
(SELECT COUNT(*) FROM replies WHERE replies.reply_blog_id = blogs.blog_id)
) AS total_count
FROM blogs;
This gives me the right result but also gives me the number of rows, I don’t know why this happens to me.
I have researched many but I didn’t get the right place, can anyone tell me where I am wrong, or there is any other query to calculate all rows in a particular blog.
I know the php-comment mysqli_num_rows($result)
to count the rows but this will happen only when we have data in the number of rows.
I want to calculate the total numbers count
of comments and replies
combined in blog1
Renuka sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1