By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,327 Members | 2,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,327 IT Pros & Developers. It's quick & easy.

mysql for select using sum in another table

P: 45
guys, please help

i need balance amount of fees all students has to pay. Following is the DB structure. `feepayid` is primary key for paymentstable and paymentregister & `studentid` is primary in studentstable. I have written a query for this, please evaluate the query.

please see the attachment for the db structure

Expand|Select|Wrap|Line Numbers
  1. SELECT d.`studentid`, SUM(a.`totalfee` - (SELECT SUM(`feeamount`) FROM `paymentstable`  WHERE a.`feepayid` = `feepayid`)) AS balancefee
  2. FROM `studentstable` d
  3. LEFT JOIN `paymentregister` a ON a.`studentid` = d.`studentid`
  4. GROUP BY d.`studentid`;
Attached Images
File Type: jpg db.jpg (56.2 KB, 137 views)
Mar 8 '12 #1
Share this Question
Share on Google+
1 Reply

P: 13,264
Explain the business rules a bit more. Which table contains the amounts that students are supposed to pay and which one contains the amounts that students actually paid?
Mar 8 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.