来自mySQL结果的总计

时间:2012-09-07 09:09:44

标签: mysql codeigniter

在codeiginter中 - 是否可以使用Active记录从mySQL中的结果中聚合总计?

我的表:

+-----------------+---------------+----------+
|  transaction_ID |  category_ID  |  amount  |
+-----------------+---------------+----------+
|        1        |      2        |   5.0    |
+-----------------+---------------+----------+
|        2        |      1        |   6.0    |
+-----------------+---------------+----------+
|        3        |      2        |   4.1    |
+-----------------+---------------+----------+
|        4        |      3        |   1.2    |
+-----------------+---------------+----------+
|        5        |      1        |   7.0    |
+-----------------+---------------+----------+

我想要实现的目标:

+---------------+----------------+
|  category_ID  |  total_amount  |
+---------------+----------------+
|       1       |      13.0      |
+---------------+----------------+
|       2       |      9.1       |
+---------------+----------------+
|       3       |      1.2       |
+---------------+----------------+

1 个答案:

答案 0 :(得分:1)

试试这个,

SELECT category_ID, SUM(amount) totalAmount
FROM tableName
GROUP BY categoryID