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`;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.