User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 426,593 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,671 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser: Programming Forums
Views: 1042 | Replies: 3
Reply
Join Date: Jan 2006
Posts: 5
Reputation: Asiima is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Asiima Asiima is offline Offline
Newbie Poster

MySQL SUM Statemene

  #1  
Apr 19th, 2008
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;
Last edited by peter_budo : Apr 20th, 2008 at 6:36 am. Reason: Keep It Organized - please use [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 114
Reputation: mwasif is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 14
mwasif mwasif is offline Offline
Junior Poster

Re: MySQL SUM Statemene

  #2  
Apr 19th, 2008
Try
  1. SELECT SUM(i.amount) - SUM(p.amount) FROM income i, payments p
Last edited by mwasif : Apr 19th, 2008 at 3:15 pm.
Reply With Quote  
Join Date: Jan 2006
Posts: 5
Reputation: Asiima is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Asiima Asiima is offline Offline
Newbie Poster

Re: MySQL SUM Statemene

  #3  
Apr 19th, 2008
Originally Posted by mwasif View Post
Try
  1. SELECT SUM(i.amount) - SUM(p.amount) FROM income i, payments p


Thanx but still doesn't work out
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: MySQL SUM Statement

  #4  
Apr 19th, 2008
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MySQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the MySQL Forum

All times are GMT -4. The time now is 10:48 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC