包括用户评论过的帖子

时间:2013-05-07 15:44:35

标签: mysql sql

我正在计算撰写帖子的用户的平均积分。但是,现在我想平均他们评论的帖子的分数。我的查询,不包括评论,如下所示:

SELECT u.user_fullname, ROUND(AVG(p.total_points),2) avgPoints
FROM cl_user_identities u
JOIN cl_posts p ON p.user_identity_id = u.user_identity_id
GROUP BY u.user_identity_id

如何添加用户评论过的帖子?

这是我的表架构:

cl_posts
 - post_id
 - user_identity_id
 - post_title
 - total_points

cl_comments
 - comment_id
 - post_id
 - user_identity_id
 - comment_text

cl_user_identities
 - user_identity_id
 - user_fullname

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

SELECT u.user_fullname, 
 ROUND(AVG(p.total_points),2) avgPoints,
 (SELECT ROUND(AVG(total_points),2) FROM cl_posts p2 JOIN cl_comments c2 ON c2.post_id = p2.post_id WHERE c2.user_identity_id = u.user_identity_id) as avgPoints2
FROM cl_user_identities u
JOIN cl_posts p ON p.user_identity_id = u.user_identity_id
GROUP BY u.user_identity_id