简体   繁体   English

MySQL根据值选择多个ID

[英]MySQL selecting more than one id depending on value

I'm working on stock control in my system and i have a table for "locations" of stock in a warehouse. 我正在系统中进行库存控制,并且有一个用于存储仓库中“位置”的表。 This is the location table. 这是位置表。

id  location_description_id location_key    product_id  qty
6   1                       1               3           25
7   1                       2               4           25
8   1                       3               3           20

I'm struggling with getting the location values. 我正在努力获取位置值。 For example if there is an order for 40 pieces for product id "3", you can see there is 25 pieces in location_key 1 and 20 pieces in location_key 3 例如,如果订购了40件产品ID为“ 3”的订单,则可以看到location_key 1中有25件,location_key 3中有20件

What would be the best way to select the next available location_key when the first one runs out? 当第一个可用的location_key耗尽时,最好的方法是什么? so in this case the return would be id 6, qty 25 and id 8, qty 15 (totaling my 40) 因此,在这种情况下,回报将是ID 6,数量25和ID 8,数量15(总计40)

Do it on PHP side: 在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');
    }
}

Assuming you have result from query to select all locations where the ordered product is stored. 假设您有查询结果,可以选择存储订购产品的所有位置。 You can do this in php. 您可以在php中执行此操作。

Edit: edited the function to also return the quantity to pick from every location. 编辑:编辑了该函数,还返回了从每个位置提取的数量。

function findLocations($locations, $orderQty)
{
$resultLocs = array();
$i = 0;
$orderRemainQty = $orderQty;

while ($orderRemainQty != 0) {
    $locQty = $locations[$i]['qty'];
    If ($orderRemainQty > $locQty){
        $locations[$i]['pickUpQty'] = $locQty;
        $orderRemainQty -= $locQty;
    } else {
        $locations[$i]['pickUpQty'] = $orderRemainQty;
        $orderRemainQty = 0;
    }
    $resultLocs[] = $locations[$i];
    $i++;
}
return $resultLocs;

} }

There is a concept called OVER (PARTITION BY id) in SQL server which will result id, qty and we can query again to get proper result. SQL服务器中有一个称为OVER(PARTITION BY id)的概念,它将产生id,qty,我们可以再次查询以获得正确的结果。

Its hard to get required response in MySQL but below link will help. 在MySQL中很难获得所需的响应,但是下面的链接会有所帮助。

-> https://explainextended.com/2009/03/10/analytic-functions-first_value-last_value-lead-lag/ -> https://explainextended.com/2009/03/10/analytic-functions-first_value-last_value-lead-lag/

you should be able to generate output like below and from that you can query sum_all>=40 limit 1; 您应该能够生成如下所示的输出,并可以从中查询sum_all> = 40限制1;

id location_key product_key qty sum_all
6  1            3           25  25
8  3            3           20  45 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM