Php 如何使用活动记录模式在codeigniter中运行此MySQL查询
我正在使用ci并使用其活动记录模式访问数据库。我想使用如下语句更新表Php 如何使用活动记录模式在codeigniter中运行此MySQL查询,php,mysql,codeigniter,activerecord,Php,Mysql,Codeigniter,Activerecord,我正在使用ci并使用其活动记录模式访问数据库。我想使用如下语句更新表 UPDATE employee_barcode set `count` = `count` + 1 where barcode_id = 2 我试着像这样使用update语句 $data = array( 'count' => 'count' + 1, ); $this->db->where('barcode_id', 2); $this->db->up
UPDATE employee_barcode set `count` = `count` + 1
where barcode_id = 2
我试着像这样使用update语句
$data = array(
'count' => 'count' + 1,
);
$this->db->where('barcode_id', 2);
$this->db->update('employee_barcode', $data);
但结果是错误的
我该怎么做呢?这不起作用,因为$This->db->update无法获取count的值,因此无法向其中添加1。您应该使用$this->db->select获得计数值,然后继续更新该值
例如:
$this->db->where('barcode_id', 2);
$this->db->select('count');
$query = $this->db->get('employee_barcode');
$row = $query->row();
$this->db->where('barcode_id', 2);
$this->db->update('employee_barcode', array('count' => ($row->count + 1)));
试试这个
$this->db->set('count', '`count+1`', FALSE)
$this->db->where('barcode_id', 2);
$this->db->update('employee_barcode');