DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   MySQL (http://www.daniweb.com/forums/forum126.html)
-   -   MySQL SUM Statemene (http://www.daniweb.com/forums/thread119963.html)

Asiima Apr 19th, 2008 12:22 pm
MySQL SUM Statemene
 
Hi there,

Anyone to help me on sql code for SUM.
I need a query that gets the SUM of one table column and subtract it from the sum from another table column:
I tried this but it gave me wrong answer;

SELECT SUM(i.amount-p.amount) FROM income i, payments p;

mwasif Apr 19th, 2008 3:13 pm
Re: MySQL SUM Statemene
 
Try
SELECT SUM(i.amount) - SUM(p.amount) FROM income i, payments p

Asiima Apr 19th, 2008 5:19 pm
Re: MySQL SUM Statemene
 
Quote:

Originally Posted by mwasif (Post 588753)
Try
SELECT SUM(i.amount) - SUM(p.amount) FROM income i, payments p



Thanx but still doesn't work out

tesuji Apr 19th, 2008 9:24 pm
Re: MySQL SUM Statement
 
hi,
without where clause cross product of both tables will be computed, that is each row of income will be combined with each row of payments. To prevent this, you need a where clause like where i.fieldofincome = p.fieldofpayments, If you don't have such common fields you can't join both tables.

krs,
cliff


All times are GMT -4. The time now is 11:20 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC