Query returning divergent result

Asked

Viewed 35 times

0

I have two tables:

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

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

SELECT SUM(accounts.value) the totals , SUM(accounts_receive.value) the totalentries FROM accounts, accounts_receive

However, the total entries I receive from the query are 1000 and not 500. Why does this happen?

  • You have to split it in two darlings. One to add up the total table contas and another to add up the total of contas_receber. This is probably because of this, since there is no relationship between the consultations.

2 answers

0


You cannot enter more than one table in the clause from without relating them in any way (using join for example). That way, you need separate results. You can use a union for example:

select 'Saidas',SUM(contas.valor) as Total
from contas
union
select 'Entradas',SUM(contas_receber.valor) as Total
from contas_receber

0

SELECT SUM(C.VALOR), SUM(CR.VALOR) FROM CONTAS C, CONTAS_RECEBER CR WHERE <CONDITION>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.