Anton Prio Hutomo Anton Prio Hutomo - 4 months ago 30
SQL Question

get difference value from 2 table

I have two tables named

T1
and
T2
. Both of the tables have a column in common called balance. How can I get the difference between the two sums of the two tables.

Example:

T1

balance
-------
100
50


The sum for
T1
would be 150 (100 + 50)

T2

balance
-------
100
200


The sum for
T2
would be 300 (100 + 200)

So I would like the output to give me the result 150 (sumT1-sumT2).

Answer

Just sum and select from tables ..

select (select ifnull(sum(balance),0) from t2)-(select ifnull(sum(balance),0) from t1) as balance
Comments