Codeigniter中SQL列的总和 [英] SQL sum of columns in Codeigniter

查看:49
本文介绍了Codeigniter中SQL列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL表,如下所示:

I have a an SQL table as follows:

 >--------------------------  
>|ID | AMOUNT | PRODUC_ID |  
>--------------------------  
>|1  | 100    | 5   
>|2  | 100    | 5  
>|3  | 100    | 5  
>|4  | 100    | 10  
>|5  | 100    | 10  
>|6  | 100    | 10 

>|6  | 100    | 10

Im使用codeigniter,Im希望根据PRODUCT_ID动态获得AMOUTS的总和。所需的输出是:

Im using codeigniter, Im expecting to get the SUM OF AMOUTS according to the PRODUCT_ID dynamically. the required output is:

>sum of prodcut_id 5 = 300

>sum of prodcut_id 10 = 400


推荐答案

使用分组依据求和。

剪切粘贴从这里

$query = $this->db->query('select produc_id, sum(amount) as total_amt from mytable group by produc_id');

foreach ($query->result() as $row)
{
    echo $row->produc_id;
    echo $row->total_amt;
}

这篇关于Codeigniter中SQL列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆