如何计算每个用户发布的总帖子数

时间:2011-12-13 19:12:46

标签: php mysql sql

我想计算每个成员的总帖子数,例如

会员表,

member_id
1
2
3
4
5
6

帖子表,

post_id         member_id
1               1
2               1
3               1
4               2
5               3

我追求的结果,

member_id       totalPost
1               3
2               1
3               1
4               0
5               0
6               0

我的工作查询,

SELECT *
FROM root_members_cfm  AS m

LEFT JOIN root_posts AS n
ON n.member_id = m.member_id

1 个答案:

答案 0 :(得分:3)

select
   count(post_id) as totalPost,
   m.member_id
from
   member as m 
left join 
   post on post.member_id=m.member_id 
group by m.member_id;