I need to get data from 3 tables in the same SELECT. I need only get the user data from the users table, get the amount of quotas (column quantity_cotas) that the user has from the quotas table (if you have registration in that user table) and the amount of invoices the user has in the invoices table (if you have one too). I tried to do this:
SELECT u.login, SUM( c.quantidade ) AS qtd , SUM( COALESCE( f.status, 1, 0 ) ) AS qtdF
FROM usuarios AS u
LEFT JOIN cotas AS c ON c.id_user = u.id
LEFT JOIN faturas AS f ON f.id_user = u.id
But it returns me twice the amount in the qtd and qtdF columns.
It was for me to return type:
alissonacioli | 14 | 2
And he's coming back to me
alissonacioli | 28 | 4
I do not know if I have to use subquery
in this case, and if I have it, I do not know how to do it, but I'm having a problem; /