다음 SQL 쿼리가 있습니다.MYSQL SUM 및 substract
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
나의 목표는 SUM 2 필드를 뺀 다음 빼기 위의 쿼리를 실행할 때 두 번째 SUM이 잘못되었습니다.
표는 다음과 같습니다 : 25 spPoints에서 : 포인트에서
points: pointId, userId, points
sppoints: spPointId, userId, spPoints
내가이 금액이 10 을하지만 난 쿼리를 실행할 때 얻을 :
points spPoints Puntos_Restantes
25 30 5
잘못 여기서 뭐하는거야 ?
당신은 당신이에서 당기는하고있는 테이블을 보여줄 수 있습니까? 아니면 너무 큽니까? – Dancrumb
은 어쩌면 당신은 합 ((p.points) (sp.spPoints))를 의미한다. – xQbert
귀하의 예제를 재현하는 데 필요한 전체 데이터를 표시하십시오 - – lanzz