我正在寻找一个要批量更新的查询,使用单个查询而不是循环中的查询。我有2000多种产品,我必须更新它们的股票价值。现在我为每个产品使用循环,所以它花费了太多的时间。以下是我的代码
foreach($products as $srcProduct){
$product_id = $srcProduct['product_id'];
$onhand = 0;
if(isset($srcProductOnhand[$product_id])){
$onhand = $srcProductOnhand[$product_id];
}
TransferSourceProducts::where(['product_id' => $product_id, 'transfer_id' => $id])
->update(['onhand' =>$onhand]);
}发布于 2019-12-18 21:03:42
您可以使用ON复制密钥更新。它的运行速度很快。
INSERT into `table` (id, description)
VALUES (1, 'desc1'), (2, 'desc2'), (3, 'desc3')
ON DUPLICATE KEY UPDATE description = VALUES(description)希望能有所帮助。
https://stackoverflow.com/questions/59392351
复制相似问题