Jump to content

mysql for select using sum in another table


charles07

Recommended Posts

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.

 

paymentstable

feepayidclassidpaydatepaymodefeeamountremarksfeediscount

3282012-03-06 19:32:351500hgfhf0

3182012-03-04 19:32:351800hgfhf0

3082012-02-06 19:32:3511200hgfhf0

2982012-02-06 19:32:3511100hgfhf0

 

paymentregister

feepayidpayidtotalfeestudentid

32315002

31235002

30145004

29355004

 

studentstable

studentidstudentname

2john

4mathew

5peter

6mary

 

SELECT d.`studentid`, SUM(a.`totalfee` - (SELECT SUM(`feeamount`) FROM `paymentstable`  WHERE a.`feepayid` = `feepayid`)) AS balancefee
FROM `studentstable` d
LEFT JOIN `paymentregister` a ON a.`studentid` = d.`studentid`
GROUP BY d.`studentid`;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.