如何将此原始 SQL 查询编写为 CodeIgniter 活动记录查询?
UPDATE products
SET current_stock = current_stock + 1
WHERE product_id = 1
您可以像这样使用 update_batch :
$products = $this->db->get('products'))->result();
for( $i = 0; $i < count($products); $i++){
$data[] = array(
'product_id' => $product[$i]->product_id,
'current_stock' => $product[$i]->current_stock + 1
);
}
$this->db->update_batch('products', $data, 'product_id');
您可以按照 Codeigniter 查询生成器类中的说明使用以下查询:
$this->db->set('current_stock', 'current_stock+1', FALSE);
$this->db->where('product_id', 2);
$result = $this->db->update('products');