2015-07-22 189 views
0

我需要根据同一个表中的另一列值计算列值,并且无法在文档中找到如何操作。我的代码如下:根据另一列值计算列值

$toChange = json_decode($request->toSend); 
     $database = $request->user()->id + 1; 
     Config::set('database.connections.usertable.database','usertable' . $database); 
     $insert = new Products(); 
     foreach ($request->countries as $key => $value) { 
      if ($value === true) { 
       foreach ($toChange as $change) { 
        $insert->setTable($key . '_products'); 
        $insert->whereBetween('price',[$change->priceRangeFrom,$change->priceRangeTo])->update([ 
         'ip_address' => $request->getClientIp(), 
         'opt_active' => 1, 
         // next line is the problem 
         'price' => 'price'/100 * (100 - $change->minPrice) 
        ]); 

       } 

      } 
     } 

我更新几千项与和需要从每一列得到的价格字段中的值,并从中计算出新的价格。有人可以建议如何做?

查询应该是这样的PDO查询

$stmt = $pdo->prepare("UPDATE uk_products SET min_price = (price * ((100 - :min)/100)) /* ... */ WHERE price BETWEEN :minrange AND :maxrange"); 
       $stmt->execute([':min' => $data['min'] /* ... */ ]); 

哪里,这是有问题的计算

min_price = (price * ((100 - :min)/100)) 

回答

0

这将工作,如果别人需要它

'min_price' => DB::raw('price/100 * (100 - '.$change->minPrice.')') 
0

为什么不预先计算值并将最终值传递给PDo语句。

$cal = min_price = (price * ((100 - :min)/100)) ; 
$stmt = $pdo->prepare("UPDATE uk_products SET min_price = :cal_price) /* ... */ WHERE price BETWEEN :minrange AND :maxrange"); 
$stmt->execute([':cal_price' => $cal /* ... */ ]);