2017-03-13 106 views
0
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 

從上面我節省了費用支付細節選擇MySQL查詢不同sumof具有

一個學生作爲支付部分利用這一點,我需要獲得上述值

量總費用和總支付的費用,餘額需要支付

爲了這個,我用

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

查看https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple- sql-query – Strawberry

+1

SELECT fee_amount as fee,SUM(fee_paid)as paid,fee_type FROM tbl_fee A WHERE fee_user ='105' group by fee_type having fee_amount!=(SUM(fee_paid),它會解決您的問題,我認爲。 – Bharath

回答

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 
0

如果你不想求和fee_amount場,那麼不要總結它,但要麼將它包含在group by子句中,要麼使用any_value()函數使您的查詢符合sq L標準:

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在費用類型和用戶中可能不同,那麼您需要指定如何處理這種情況。