代码点火器数据库计算列平均值 [英] Code igniter database calculate column average
本文介绍了代码点火器数据库计算列平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在函数中计算数据库表"score"中名为"totalscore"的列的平均得分 我试图做活动记录select_avg(),但是我什么也没得到. 知道我该怎么做吗?
I need to calculate in a function the average score of a column named: "totalscore" from my database table "score" I tried to do Active record select_avg() but I am not getting anything. Any idea how I can do this?
function calculateaverage(){
$dataArr = array();
$data = $this->db->get('score');
$maxrows = $data->num_rows();
$data = $this->db->get('score');
for ($i = 1; $i<= $maxrows-1; $i++){
$this->db->select('totalscore');
foreach ($data->result() as $row) {
$dataArr[$i] = $row->totalscore;
}
}
return $dataArr;
}
推荐答案
您可以尝试此代码,非常简单明了.将其写入模型中.在控制器中使用,例如$this->yourmodel->calculateaverage;
基本上,我们告诉Codeigniter查询构建器选择totalscore
的AVG
.
You can try this code, very simple and straight forward. write it in your model. use in Controller like $this->yourmodel->calculateaverage;
basically we are telling codeigniter query builder to select the AVG
of our totalscore
..
function calculateaverage(){
$query = $this->db->select('AVG(totalscore) as average_score')->from('score')->get();
return $query->row()->average_score;
}
这篇关于代码点火器数据库计算列平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文