我有一个大但有些简单的 SQL 查询。基本上,我网站上的用户会为不同类型的活动建立声誉,例如撰写评论、发表评论以及向我们的数据库添加条目。在大多数情况下,这些点存储在 reputable_actions 表中,我通过重复左连接 reputable_actions 表来检索它们。这感觉很草率,但它主要是有效的。
我遇到的问题是两个声誉,“审稿人”和“社区”。与其他人不同,它们不存储在 reputable_actions 表中。相反,它们的值来自投票表,我首先通过左连接评论表来访问该表。出于某种原因,加入评论表会导致我所有其他的声誉成倍增加。在一次试验中,“档案管理员”的声誉应该是 25,但当我加入评论时,它飙升至 10050。
我是 SQL 的新手,我已经尝试了我所知道的(即,将 GROUP BY 子句应用于 users.id),但我还没有运气。一些指导将不胜感激。
SELECT users.*,
SUM(COALESCE(reviewers.value, 0)) as reviewer,
SUM(COALESCE(communities.value,0)) as community,
SUM(COALESCE(developers.value,0)) as developer,
SUM(COALESCE(moderators.value,0)) as moderator,
SUM(COALESCE(marketers.value,0)) as marketer,
SUM(COALESCE(archivists.value,0)) as archivist,
SUM(COALESCE(karmas.value,0)) as karma
FROM `users`
LEFT JOIN comments AS impressions
ON impressions.user_id = users.id
AND impressions.type = 'impression'
LEFT JOIN comments AS replies
ON replies.user_id = users.id
AND replies.type = 'reply'
LEFT JOIN votes AS reviewers
ON reviewers.voteable_type = 'impression'
AND reviewers.voteable_id = impressions.id
LEFT JOIN votes AS communities
ON communities.voteable_type = 'reply'
AND communities.voteable_id = replies.id
LEFT JOIN reputable_actions AS developers
ON developers.reputation_type = 'developer'
AND developers.user_id = users.id
LEFT JOIN reputable_actions AS moderators
ON moderators.reputation_type = 'moderator'
AND moderators.user_id = users.id
LEFT JOIN reputable_actions AS marketers
ON marketers.reputation_type = 'marketer'
AND marketers.user_id = users.id
LEFT JOIN reputable_actions AS archivists
ON archivists.reputation_type = 'archivist'
AND archivists.user_id = users.id
LEFT JOIN reputable_actions AS karmas
ON karmas.reputation_type = 'karma'
AND karmas.user_id = users.id
GROUP BY users.id