query of 1 table related to 3 mysql

0

I have 3 tables: 1. contributions 2. contributions_volunteers 3. contributions_multas All these tables have an import field, and they are all related to the table: 4. associates What I want is to show the partner and the sum of their amounts in a single query (and that in a single line I show all the contributions for each associate) I enclose the query that I made, but that does not bounce me the correct results I throw out figures that do not correspond

SELECT A.id_asociados, 
       Concat(A.nombres, ' ', A.a_paterno, ' ', A.a_materno) AS socios, 
       Sum(B.importe)                                        AS 'obligatorio', 
       Sum(C.importe)                                        AS 'voluntario', 
       Sum(M.importe)                                             AS 'multas' 
FROM   asociados A 
       INNER JOIN aportes B 
               ON A.id_asociados = B.id_asociados 
       INNER JOIN aportes_voluntarios C 
               ON A.id_asociados = C.id_asociados 
       INNER JOIN aportes_multas M 
               ON A.id_asociados = M.id_asociados 
GROUP  BY A.id_asociados 
    
asked by Fernando Abel Gonzales Ch 08.09.2018 в 16:45
source

1 answer

0
SELECT  A.id_asociados,concat(A.nombres,' ',A.A_paterno,' ',A.A_materno) as socio,
    A.dni,B.obligatorio,C.voluntario,D.multas
    FROM asociados A
    LEFT JOIN (SELECT id_asociados,
                  SUM(importe)  AS 'obligatorio'
                  FROM aportes
                  GROUP BY id_asociados
            ) B
        ON A.id_asociados = B.id_asociados
                  LEFT JOIN (SELECT id_asociados,
                               SUM(importe)  AS 'voluntario'
                               FROM aportes_voluntarios
                               GROUP BY id_asociados
                          ) C
                         ON A.id_asociados = C.id_asociados
                             LEFT JOIN (SELECT id_asociados,
                                          SUM(importe)  AS 'multas'
                                          FROM aportes_multas
                                          GROUP BY id_asociados
                                      ) D
                                      ON A.id_asociados = D.id_asociados   
 GROUP BY A.id_asociados,B.obligatorio,C.voluntario,D.multas

I RESOLVED IT THIS WAY (in case someone serves)

    
answered by 08.09.2018 в 18:13