在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 |
+---------------+----------------+
答案 0 :(得分:1)
试试这个,
SELECT category_ID, SUM(amount) totalAmount
FROM tableName
GROUP BY categoryID