简体   繁体   English

如何在Codeigniter中从数据库的一个表中的一列乘以值?

[英]How to multiply value from one column in one table from database in codeigniter?

I want to multiply value from table after I selected from database in one column and table using forearch loops 我想使用forearch循环从一列和表中的数据库中选择表后将表中的值相乘

Here is my selection data from database and I don't know to multiply total value in its column 这是我从数据库中选择的数据,我不知道将其列中的总值相乘

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();
        }
    }

the result I want to total my value as below images. 我想要将我的价值总计如下图的结果。

在此处输入图片说明

Loop over the results and multiply it: 循环结果并将其相乘:

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

However, that won't make 2*2*2*2*5 equal 48... maybe if there was a 3 instead of that 5... 但是,这不会使2 * 2 * 2 * 2 * 5等于48 ...也许如果有3而不是那个5 ...

Try this one , you can get the result directly without looping (numbers should be only positive) 试试这个,您可以直接获得结果而无需循环(数字应该仅是正数)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在codeigniter的mysql数据库的表的一列中将来自3个不同输入字段的数据一起插入 - How to insert data from 3 different input fields together in one column of table in mysql database in codeigniter 将列从一个表连接到另一个表CodeIgniter中的另一列 - Join column from one table to another column in other table CodeIgniter 在codeigniter中将数据库内容从一个表复制到另一个表 - Copy database contents from one table to another table in codeigniter Codeigniter:如何从数据库中的表中移动值? - Codeigniter : How to move value from table in database? 如何在PHP Codeigniter中从一个表中获取特定列,并从另一表中获取所有其他数据 - How to get specific column from one table and all other data from other table in php codeigniter 如何将每列中的 2 个值输入到另一个表中的一列 - how to input 2 value from each column to one column in another table 如果一个表列为空,如何从另一表获取值 - How to get the value from the other table if one table column is empty 如何在 Codeigniter 中将所有数据从一个数据库克隆到另一个数据库 - How clone all data from one database to another one in Codeigniter Codeigniter 从数组中增加数据库中的值,仅转义一个组件 - Codeigniter increase value in database from array with only one component escaped 在Codeigniter中从联接表中显示表外的一个值 - Showing one value outside the table from joined tables in Codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM