如何获得个人身份证的所有总和?

时间:2016-02-25 09:32:57

标签: php mysql codeigniter sum

我有一个包含st_id列的表格。

每列都有amount.eg jan_1,feb_1有两行。一个用于jan_2,feb_2数据,另一个用于SELECT st_id,SUM(jan_1+feb_1) as amt1,SUM(jan_2+feb_2) as amt2 FROM fee_amount WHERE `st_id` IN (5,7,8); 数量。

我想要每个id的一行。现在我只得到了所有金额的一行。

查询:

autoHighlight="true"

4 个答案:

答案 0 :(得分:1)

您需要GROUP BY st_id。因此,您将获得每个st_id的每一行。

SELECT 
 st_id,
 SUM(jan_1+feb_1) as amt1,
 SUM(jan_2+feb_2) as amt2 
FROM fee_amount WHERE `st_id` IN (5,7,8)
GROUP BY `st_id`;

注意:没有Group By的聚合函数会将结果集折叠为一行。

See Demo

答案 1 :(得分:0)

在您的查询中,最后只添加GROUP BY子句( GROUP BY st_id )。

SELECT 
 st_id,
 SUM(jan_1+feb_1) as amt1,
 SUM(jan_2+feb_2) as amt2 
 FROM fee_amount 
 WHERE `st_id` IN (5,7,8)
 GROUP BY `st_id`;

答案 2 :(得分:0)

合理的方法是将(jan_1 + jan2)求和为Jan,同时对feb数据进行相同的处理。

以下是查询:

SELECT 
    st_id,
    SUM(jan_1 + jan_2) AS jan,
    SUM(feb_1 + feb_2) AS feb
FROM
    fee_amount 
WHERE 
    `st_id` IN (5,7,8)
GROUP BY
    st_id;

答案 3 :(得分:0)

SELECT st_id,
       Sum(jan_1 + feb_1) AS amt1,
       Sum(jan_2 + feb_2) AS amt2
FROM   fee_amount
WHERE ` st_id ` IN ( 5, 7, 8 )
GROUP  BY ` st_id ` ;