我有这样的数据
category expense monthyear
--------------------------------------------------------------------------
1. food 1000 Dec2015
2. movie 100 Dec2015
3. housing 2000 Dec2015
4 .food 1000 Dec2015
5 .housing 2000 Dec2015
6. food 1000 Dec2015
当我插入数据库时,我希望得到每个类别费用的总和,如:food-3000,movie-100,housing-4000
如何编写查询,即时在codeigniter
框架中进行?
答案 0 :(得分:4)
您需要的基本查询结构是:
INSERT INTO expensetotals (category_total, exp_total)
SELECT category AS category_total, SUM(expense) AS exp_total
FROM expenses
GROUP BY category
新表格“expensesetotals”如下所示:
category_total exp_total
------------------------------
food 3000
housing 4000
movie 100
答案 1 :(得分:1)
我不熟悉CodeIgnitor,但根据您的问题,您可以尝试此查询
INSERT INTO categorysum(category, amount) SELECT category, sum(expense) FROM current_table WHERE 1 GROUP BY category