-2

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

1 回答 1

1

试试这个,

SELECT category_ID, SUM(amount) totalAmount
FROM tableName
GROUP BY categoryID
于 2012-09-07T09:11:12.860 回答