table structure
=====================================================================
id |fee_amount|fee_paid|fee_type|fee_user|date | Fee_for
=====================================================================
1 | 2000 | 500 | REG | 105 | 01.02.2017 | FEB
-----------------------------------------------
2 | 2000 | 1000 | REG | 105 | 03.02.2017 | FEB
-----------------------------------------------
3 | 2000 | 500 | REG | 105 | 04.02.2017 | FEB
-----------------------------------------------
4 | 1000 | 500 | FEE | 105 | 10.03.2017 | MAR
-------------------------------------------------------------
5 | 1000 | 500 | FEE | 105 | 11.03.2017 | MAR
--------------------------------------------------------------
6 | 1000 | 1000 | FEE | 105 | 13.03.2017 | APR
从上面我节省了付费详情
一名学生的部分金额是按上述价值支付的
使用这个我需要获得总费用和总付费以及需要支付的余额
为此我用了
SELECT SUM(fee_amount) as fee, SUM(fee_paid) as paid ,fee_type
FROM tbl_fee A WHERE fee_user='105'
group by fee_type
having SUM(fee_amount)!=(SUM(fee_paid)
但它得到了
6000 | 2000 | REG
2000 | 1000 | FEE
我需要
2000 | 2000 | REG
1000 | 1000 | FEE
答案 0 :(得分:0)
只需使用此查询即可获得所需的结果
SELECT fee_amount as fee, SUM(fee_paid) as paid ,fee_type FROM tbl_fee A WHERE fee_user='105' group by fee_type
答案 1 :(得分:0)
如果您不想对fee_amount
字段求和,那么就不要总结,但要么将其包含在group by
子句中,要么使用any_value()
函数来制作查询符合sql标准:
SELECT fee_amount as fee, fee_type, SUM(fee_paid) as paid
FROM tbl_fee A
WHERE fee_user='105'
group by fee_type, fee_amount
显然,这些解决方案假设fee_amount
在所有用户的fee_type中是相同的。如果fee_amount
在费用类型和用户中可能不同,那么您需要指定如何处理这种情况。