我有以下 SQL 查询。
SELECT u.username, SUM(p.points) AS points,
SUM(sp.spPoints) AS spPoints,
(SUM(sp.spPoints) - SUM(p.points)) AS Puntos_Restantes
FROM users as u
LEFT JOIN points as p ON (u.userid = p.userid)
LEFT JOIN sppoints AS sp ON (u.userid = sp.userid)
WHERE u.userid = '1'
GROUP BY u.userid
我的目标是求和 2 个字段,然后减去它们,但是当我执行上述查询时,第二个 SUM 是错误的。
表格是这样的:
points: pointId, userId, points
sppoints: spPointId, userId, spPoints
在点我有这个数量:25 和 spPoints:10 但是当我运行查询时,我得到:
points spPoints Puntos_Restantes
25 30 5
这里出了什么问题?