I have the following SQL query.
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
My goal is to SUM 2 fields and then subtract them but when I execute the above query, the second SUM is wrong.
The tables are like this:
points: pointId, userId, points
sppoints: spPointId, userId, spPoints
In points I have this amount: 25 and in spPoints: 10 but when I run the query I get :
points spPoints Puntos_Restantes
25 30 5
What is going wrong here?