【问题标题】:Codeigniter Active record queryCodeigniter 活动记录查询
【发布时间】:2016-03-10 21:46:24
【问题描述】:

如何将此程序查询编写为 codeigniter 活动记录查询(update_batch 模式)

  UPDATE products SET current_stock = current_stock + 1 where product_id = 1

【问题讨论】:

  • 您不需要为此使用 update_batch。当您想在一个查询中更改多条记录时,更新批处理很有用。例如,在您的查询中,如果您想更改情侣产品的库存价值,那么您可以使用 update_batch 来完成。
  • 我要更新多条记录。
  • 好的,我更改了查询。我认为最好在标题中添加“update_batch”,以便对其他人有用。

标签: sql codeigniter activerecord


【解决方案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');

【讨论】:

    【解决方案2】:

    您可以按照Codeigniter Query Builder Class 中的说明使用以下查询:

    $this->db->set('current_stock', 'current_stock+1', FALSE);
    $this->db->where('product_id', 2);
    $result = $this->db->update('products');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      相关资源
      最近更新 更多