MYSQL sum()返回双得分

时间:2017-04-21 09:34:47

标签: mysql

我使用以下查询来制作排行榜,但由于某种原因,它会将得分作为值的两倍返回。

SELECT p.user_id, u.first, SUM(points) AS score FROM points AS p LEFT 
JOIN users AS u ON p.user_id = u.fb_id WHERE p.action_time > 
'1492732800' GROUP BY p.user_id ORDER BY points DESC LIMIT 10

可能导致这种情况发生的原因是什么?

由于

1 个答案:

答案 0 :(得分:1)

一种解决方法是在单独的子查询中聚合点并加入到:

SELECT t1.fb_id, t1.first, t2.score
FROM users t1
INNER JOIN
(
    SELECT user_id, SUM(points) AS score
    FROM points
    WHERE action_time > '1492732800'
    GROUP BY user_id
) t2
    ON t2.user_id = t1.fb_id