如何将一个表中的一列中的值与codeigniter中的数据库相乘?

时间:2014-12-08 04:05:01

标签: php mysql codeigniter

我想在使用forearch循环从一个列和表中选择数据库后从表中乘以值

以下是我从数据库中选择的数据,我不知道在其列中乘以总值

public function total_comp_in(){

        $this->query = $this->db->get_where('prifix',array('status'=>1)); 
        if($this->query->num_rows()>0){ 
             return $this->query->result();
        }
    }

结果我希望将我的价值总计如下图所示。

enter image description here

2 个答案:

答案 0 :(得分:3)

循环结果并将其相乘:

if($this->query->num_rows()>0){ 
    $total = 1;
    $result =  $this->query->result();
    foreach($result as $row) {
        $total *= $row->total;
    } 
    return $total;
}

然而,这不会使2 * 2 * 2 * 2 * 5等于48 ...也许如果有3而不是5 ...

答案 1 :(得分:3)

试试这个,你可以直接得到结果而不需要循环(数字应该只是正数)

SELECT CAST(EXP(SUM(LOG(total))) AS UNSIGNED) AS result
FROM prifix WHERE status = 1