【问题标题】:laravel: how to multiply current value of column with some value when updating rows?laravel:更新行时如何将列的当前值与某个值相乘?
【发布时间】:2016-01-04 07:22:25
【问题描述】:

问题:

我要更新mysql表的两列,

column1 减去某个值,column2 乘以某个值,

我已经从here做这样的递减

试过了:

$stock_obj->decrement('quantity_on_hand', $product_data["quantity"], array(
    'total_quantity_on_hand' => 'quantity_on_hand' * some_other_value
));

这里减法工作,但我怎么能做乘法?

我在 CI(CodeIgniter) 中有该解决方案,但不知道如何在 laravel 中实现,任何帮助将不胜感激...

CI(CodeIgniter) 方式:

$this->db->set("quantity_on_hand", "quantity_on_hand-" . ($product_data["quantity"] ? $product_data["quantity"] : 0), FALSE);
$this->db->set("total_quantity_on_hand", "quantity_on_hand*" . ($product_data['product_packing_value'] ? $product_data['product_packing_value'] : 1), FALSE);
$this->db->where("id", $warehouse_transfer_product_data['stock_id'])->update("stock");

【问题讨论】:

  • 您是否希望 column2 为 (2*column1)?
  • @Rafiq column1 = column1 - 'dynamic_value' and column2 = column1 * 'some_other_value'
  • Nliyapra 你在用mysql吗?
  • @Rafiq yes with laravel eloquent

标签: php mysql laravel laravel-5 eloquent


【解决方案1】:

以下应该可以解决问题:

DB::table('your_table')
->where('some_column', $someValue)
->update(array(
    'column1' => DB::raw('column1 * 2'),
    'column2' => DB::raw('column2 - 1'),
));

【讨论】:

  • 那么decrement 我想要两个一起multiplicationdecrement
  • 你可以再添加一列,我会更新答案
  • 工作,但如果现在我想用这个查询在哪里放置条件
  • 只需添加 where 条件,就像在其他任何地方执行此操作一样。我会更新答案
猜你喜欢
  • 1970-01-01
  • 2013-10-18
  • 2016-01-17
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-26
相关资源
最近更新 更多