如何通过特定id计算评论总和

时间:2016-08-15 07:29:11

标签: php mysql codeigniter

我有两个表格blog,其中包含标题和说明,而第二个表格comment则显示与特定blog_id相关的所有评论。

blog_idblog表的主键,用作comment表中的外键。

2 个答案:

答案 0 :(得分:1)

您可以使用

加入,计数和分组
select a.title, b.description, c.count(*) 
from table_one as a
inner join table_two as b on a.id = b.table_one_id
group by a.title, b.description

答案 1 :(得分:1)

假设您的博客ID为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
相关问题