2014-10-01 174 views
1

兩個條件我有一個數據組通過在MySQL查詢

+----+----------+-------+---------+ 
| id | date |loan_id|daily_amt| 
+----+----------+-------+---------+ 
| 1 |20-07-2014| 1 | 200 | 
| |   |  |   | 
| 2 |20-07-2014| 1 | 300 | 
| |   |  |   | 
| 3 |20-07-2014| 2 | 400 | 
| |   |  |   | 
| 4 |20-07-2014| 2 | 200 | 
| |   |  |   | 
| 5 |21-07-2014| 1 | 100 | 
+----+----------+-------+---------+ 

我需要每天daily_amt總每個loan_id在這個結構如下表dailyentry。如下所示。

+----+----------+-------+---------+ 
| id | date |loan_id|daily_amt| 
+----+----------+-------+---------+ 
| 1 |20-07-2014| 1 | 500 | 
| |   |  |   | 
| 3 |20-07-2014| 2 | 600 | 
| |   |  |   | 
| 5 |21-07-2014| 1 | 100 | 
+----+----------+-------+---------+ 

我試圖通過groub by date得到它。這樣下面,

select *,sum(daily_amt) from dailyentry group by date

此查詢結果被示出像下面。

+----+----------+-------+---------+--------------+ 
| id | date |loan_id|daily_amt|sum(daily_amt)| 
+----+----------+-------+---------+--------------+ 
| 1 |20-07-2014| 1 | 500 |  1100  | 
| |   |  |   |    | 
| 5 |20-07-2014| 1 | 100 |  100  | 
+----+----------+-------+---------+--------------+ 

我希望有人已經解決過這樣的問題了。請分享一些想法。

+0

想要通過兩個元素gorup? – user28470 2014-10-01 12:50:52

+0

yes ...按日期以及loan_id – 2014-10-01 12:51:35

回答

2

您是否嘗試過:

select *,sum(daily_amt) from dailyentry group by date, loan_id 
+0

我試着用'group by date和loan_id'這樣的..我錯過了使用','那裏...無論如何謝謝... – 2014-10-01 13:04:14