答案 0 :(得分:3)
这是一种方法:
select u.userid, u.name,
count(distinct case when b.visibility = 1 then b.blogid end) as num_public_blogs,
count(distinct case when b.visibility = 0 then b.blogid end) as num_private_blogs,
count(case when b.visibility = 1 then bp.blogid end) as num_public_posts,
count(case when b.visibility = 0 then bp.blogid end) as num_private_posts
from users u left join
blogs b
on b.userid = u.userid left join
blogposts bp
on bp.blogid = b.blogid
group by u.userid, u.name;