Php 如何将所有类别的总和插入mysql?
我有这样的数据Php 如何将所有类别的总和插入mysql?,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有这样的数据 category expense monthyear -------------------------------------------------------------------------- 1. food 1000 Dec2015 2. movie 100 Dec2015 3. housing 2000 Dec2015
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
框架中进行的?我不熟悉codeigniter,但根据您的问题,您可以尝试此查询
INSERT INTO categorysum(category, amount) SELECT category, sum(expense) FROM current_table WHERE 1 GROUP BY category
您需要的基本查询结构是:
INSERT INTO expensetotals (category_total, exp_total)
SELECT category AS category_total, SUM(expense) AS exp_total
FROM expenses
GROUP BY category
其中新表“expensetotals”如下所示:
category_total exp_total
------------------------------
food 3000
housing 4000
movie 100
是,当我插入到数据库时,每个类别插入到另一个表的时间总和相同。两个插入查询。