我的数据库中有4个表
第一张桌子:学生
id Name roll wheree final
1 vivek 22222 leave 65000
2 abc 33333 admission 50000
第二张表:费用
id amount roll refund
1 9250 22222 no
2 5000 22222 no
2 5000 33333 no
2 7000 22222 yes
第三张表:pdc
id amount roll refund statuss
1 9250 22222 no recived
2 9250 22222 no cleared
2 6000 22222 yes recived
第四张桌子:折扣
id amount roll
1 5000 22222
2 6500 22222
2 6000 33333
我正在尝试跑步
select student.id,student.roll,student.name as
stu,student.final,ifnull(sum(gst_amount),0) as other,
(student.final-ifnull(sum(gst_amount),0)) as fee_to_paid,
ifnull(sum(fee.amount),0) as fee_deposit,(student.final-ifnull(sum(gst_amount),0)
-ifnull(sum(fee.amount),0)
-ifnull(sum(pdc.amount),0)) as fee_remain,ifnull(sum(pdc.amount),0) as pdc,count(fee.amount)
as tot_ins from student left join fee on fee.roll=student.roll
left join discount on discount.roll=student.roll left join pdc
on pdc.roll=student.roll where
wheree!='inquiry' where roll='22222' GROUP by student.roll
我想要这个输出。
id roll final other fee_to_paid fee_deposit pdc fee_remain tot_ins
1 22222 65000 11000 54000 7250 9250 37500 2
我想减少退款金额为y且与pdc相同的费用金额
答案 0 :(得分:0)
我已经阅读了您的问题,并观察了您的表格数据和查询。您尚未为费用和pdc表格指定退款金额是。 因此,请在下面的查询中尝试此操作,我已经正确了...
select student.id
,student.roll
,student.name as stu
,student.final
,ifnull(sum(gst_amount),0) as other
,(student.final-ifnull(sum(gst_amount),0)) as fee_to_paid
,ifnull(sum(fee.amount),0) as fee_deposit,(student.final-ifnull(sum(gst_amount),0)-ifnull(sum(fee.amount),0)-ifnull(sum(pdc.amount),0)) as fee_remain
,ifnull(sum(pdc.amount),0) as pdc,count(fee.amount) as tot_ins
from student left join fee on fee.roll=student.roll AND fee.refund='yes'
left join discount on discount.roll=student.roll
left join pdc on pdc.roll=student.roll AND pdc.refund='yes' AND wheree!='inquiry'
where student.roll='22222' GROUP by student.roll