我正在使用 ci 并使用它的事件记录模式来访问数据库。我想使用如下语句更新表
UPDATE employee_barcode set `count` = `count` + 1
where barcode_id = 2
我试过像这样使用更新语句
$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
获取 count
值,然后继续更新该值。
例如:
$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)));
关于php - 如何使用事件记录模式在 codeigniter 中运行此 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28372247/