模型中1个函数中的多个MySQL查询 [英] Multiple MySQL Queries in 1 Function in Model
本文介绍了模型中1个函数中的多个MySQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 是否可以在函数
中运行2个MySQL查询(选择AVG,然后更新)?
- Is it possible to run 2 MySQL Queries (select AVG and then Update) in a Function ?
- 如何使用以下命令打印/回显当前错误:$ this-> db-> error();或$ this-> db-> last_query(); ?
- 我的代码包含一个MySQL子查询,语法有误吗?
下面的代码似乎不起作用。我试图从第一个查询中获取平均值,然后将该查询用作存储(更新)到数据库的字段。请帮忙。
This below code seems like not working. I am trying to get an average value from the first query and then use the query as a field to store (update) to database. Please help..
public function updateReprob(){
$id = $this->input->post('txtId');
return $rata2 = $this->db->query('SELECT AVG(harian) a from (select harian from sla limit 3) b');
$field = array(
'harian' => $this->input->post('nameharian'),
'bulanan' => $rata2
);
$this->db->where('id', $id);
$this->db->update('sla', $field);
if($this->db->affected_rows() > 0){
return true;
}else{
return false;
}
}
推荐答案
您取得结果的方式有误。看看下面的代码
You have error in your way of getting result. take a look at below code
public function updateReprob(){
$id = $this->input->post('txtId');
$rata2 = $this->db->query('SELECT AVG(harian) a from (select harian from sla limit 3) b')->row();
$field = array(
'harian' => $this->input->post('nameharian'),
'bulanan' => $rata2->a
);
$this->db->where('id', $id);
$this->db->update('sla', $field);
if($this->db->affected_rows() > 0){
return true;
}else{
return false;
}
}
希望它对您有帮助!
这篇关于模型中1个函数中的多个MySQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文