0

我有一个脚本可以在一个简单的成就类型脚本中计算用户点数,然后按顺序将它们显示在网页上。我有以下 mySQL 函数,但如果用户还没有任何积分(即没有找到“pa.userid”,则根本不会获取用户)

SELECT u.userid,
SUM(IF(pa.plus = '1', pa.points_amount, 0)) - SUM(IF(pa.plus = '0', pa.points_amount, 0)) AS points
FROM awarded_points pa,
users u WHERE u.userid = pa.userid 
GROUP BY u.userid 
ORDER BY points DESC 
LIMIT 10

我将如何使上述代码包含尚未获得任何积分的用户?这是一个复杂的变化吗?

4

2 回答 2

1

用一个left outer join

SELECT u.userid,
       SUM(IF(pa.plus = '1', pa.points_amount, 0)) - SUM(IF(pa.plus = '0', pa.points_amount, 0)) AS points
FROM users u
LEFT OUTER JOIN awarded_points pa ON u.userid = pa.userid 
GROUP BY u.userid 
ORDER BY points DESC 
LIMIT 10

看到关于连接的精彩解释

于 2012-07-13T13:32:47.537 回答
0

使用左连接

SELECT u.userid, 
SUM(IF(pa.plus = '1', pa.points_amount, 0)) - SUM(IF(pa.plus = '0', pa.points_amount, 0)) AS points 
FROM users as u left join awarded_points pa 
ON u.userid = pa.userid  
GROUP BY u.userid  
ORDER BY points DESC  
LIMIT 10 
于 2012-07-13T13:35:30.013 回答