[英]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.