group by for mysql查询中的两个条件

时间:2014-10-01 12:47:48

标签: mysql group-by

我在以下结构中有一个日常表格,其中包含数据

+----+----------+-------+---------+
| 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总计。如下所示。

+----+----------+-------+---------+
| 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     |
+----+----------+-------+---------+--------------+

我希望有人之前已经解决了这样的问题。请分享一些想法。

1 个答案:

答案 0 :(得分:2)

你试过了吗?

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