2015-02-06 30 views
1

我正在使用ci並使用其活動記錄模式訪問數據庫。我想更新using語句表像如何使用活動記錄模式在codeigniter中運行此MySQL查詢

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); 

但結果是錯誤的。

我該怎麼做?

回答

0

這不起作用,因爲$this->db->update無法獲得值count,因此無法將1加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))); 
0

試試這個..

$this->db->set('count', '`count+1`', FALSE) 
$this->db->where('barcode_id', 2); 
$this->db->update('employee_barcode'); 
相關問題