Query returning divergent result

0

I have two tables:

Table accounts with the value column with two records totaling 1100; Table accounts_receiver with the value column with a record totaling 500

I want to perform a query where I get the sum of each table, I tried something like:

SELECT SUM (accounts.value) totals, SUM (accounts_receives.value) the total entries FROM accounts, accounts_receber

However, the total entries I receive from the query are 1000 instead of 500. Why does this occur?

    
asked by anonymous 07.02.2018 / 16:46

2 answers

0

You can not enter more than one table in the from clause without relating them in some way (using join for example). That way you need the results separated. You can use union for example:

select 'Saidas',SUM(contas.valor) as Total
from contas
union
select 'Entradas',SUM(contas_receber.valor) as Total
from contas_receber
    
07.02.2018 / 17:03
0
SELECT SUM(C.VALOR), SUM(CR.VALOR) FROM CONTAS C, CONTAS_RECEBER CR WHERE <CONDITION>
    
07.02.2018 / 17:17