我正在我的系统中进行库存控制,我有一张表用于仓库中库存的“位置”。这是位置表。
id location_description_id location_key product_id qty
6 1 1 3 25
7 1 2 4 25
8 1 3 3 20
我正在努力获取位置值。例如,如果产品 ID 为“3”的订单有 40 件,则可以看到 location_key 1 中有 25 件,location_key 3 中有 20 件
当第一个用完时,选择下一个可用 location_key 的最佳方法是什么?所以在这种情况下,返回将是 id 6,数量 25 和 id 8,数量 15(我的总计 40)
最佳答案
在 PHP 端执行:
$dbLocations = $db->prepare("SELECT * FROM location WHERE product_id = 3")->queryAll();
$totalOrder = 50;
$leftOrder = $totalOrder;
$locations = [];
while ($leftOrder >= 0) {
$location = array_shift($dbLocations);
if (!empty($location)) {
$leftOrder -= $location['qty'];
$locations[] = $location;
} else {
throw new Exception('Quantity is too big for locations');
}
}
关于php - MySQL根据值选择多个id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46583535/