【问题标题】:How to minus from several MySQL fields a certain value?如何从几个 MySQL 字段中减去某个值?
【发布时间】:2017-12-18 04:43:09
【问题描述】:

我正在编写产品库存脚本。我有一个 MySQL 表“products_stock”:

id   zid   pid  amount
1    123   321   1
2    124   321   5
4    124   566   7
3    125   321   10

因此,库存产品 id = 321 的总量为 16。有人使用 pid=321 和 qty = 7 下订单。我需要一些 php 函数,它将从第一个 zid 开始的列金额中减去 7,并且更新表 products_stock 中的记录,使其如下所示:

id   zid   pid  amount
1    123   321   0
2    124   321   0
4    124   566   7
3    125   321   9

我从这一点上被困住了。 谢谢你的回答!

【问题讨论】:

  • 如果我没猜错的话,您有一个 product_stock 表,其中包含许多条目 product_id 321,您想从所有 product_id 321 条目中减去订单数量?
  • 对不起!我不知道如何编写它,使用 php codeigniter 显示结果如下。

标签: php codeigniter codeigniter-2 codeigniter-3


【解决方案1】:

我不使用 codeigniter,而是阅读有关如何执行 select operationbatch update 的文档。主要问题是让您的逻辑正确...您选择该特定项目的所有产品条目,然后迭代并从订购的产品中减去每个产品的数量。

<?php

//your order 
$orders = [
    ['id' => 321, 'qty' => 7],
    ['id' => 501, 'qty' => 20],
];


$data = [];


foreach($orders as $order) {

    $items = $this->db->where('pid', $order['id']);
    $order_qty = $order['qty'];

    foreach($items as $item){
        $item_qty = $item->amount - $order_qty;

        if ($item_qty <= 0) {
            // set amount to 0 because it is less than order quantity
            $data[] = ['id' => $item->id, 'amount' => 0];

            // Subtract amount from order quantity to get the remaining order
            $order_qty = $order_qty - $item->amount;

        } else {
             //If amount is more than order quantity set to calculated amount
             $data[] = ['id' => $item->id, 'amount' => $item_qty];

             // update order to 0
             $order_qty = 0;
        }

        //exit if order quantity is 0
        if ($order_qty === 0 ){
            break;
        }
    }
}

$this->db->update_batch('product_stock', $data, pid'); 

【讨论】:

  • 如果有人用 pid=321 和 qty = 7 和 pid=566 和 5 下订单。我该怎么办?请帮忙,谢谢。
  • 支持一个答案就足以表示感谢。您只需要创建一个订单数组 [['pid' => 321, 'qty' => 7],['pid' => 556, 'qty' => 5]],循环并传入代码进入循环。
  • @LornTitya 我已更新代码以反映您的更新。
  • 很高兴能帮上忙
【解决方案2】:

您可以通过从 products_stock 表中选择所有相关行并在循环中一一减去/更新值来做到这一点,直到您所需的数量超过为止。

示例代码

// These are the inputs you will be getting.
$pid = 321;
$qty = 7;

// Fetch all the relevent rows using the below query statement.
$select_sql = "select * from products_stock where pid = {$pid} and amount > 0";

// This will return the below array.
$data = [
    [
        'id' => 1,
        'zid' => 123,
        'pid' => 321,
        'amount' => 1,
    ],
    [
        'id' => 1,
        'zid' => 124,
        'pid' => 321,
        'amount' => 5,
    ],
    [
        'id' => 1,
        'zid' => 125,
        'pid' => 321,
        'amount' => 10,
    ],
];

$update_data = [];

// You can then loop through your rows to perform your logic
foreach  ($data as $row) {
    // Exit loop if qty is 0
    if ($qty == 0) {
        break;
    }

    $id = $row['id'];
    $amount = $row['amount'];

    // Subtract the required amount from qty.
    $qty -= $amount;
    $amount = 0;

    // If extra qty was consumed, add back to the amount.
    if ($qty < 0) {
        $amount =+ ($qty * -1);
        $qty = 0;
    }

    // Update you data here or the best practice would be to avoid sql in a loop and do a bulk update.
    // You can do this based on your requirement.
    // $update_sql = "update products_stock set amount = {$amount} where id = {$id}";

    // Prepare date for bulk update
    $update_data []= [
        'id' => $id,
        'amount' => $amount,
    ];

    echo "Qty {$qty} | Amt {$amount} \n";
    echo "--------\n";
}

// Bulk update all your rows
if (count($update_data) > 0) {
    $this->db->update_batch('products_stock', $update_data, 'id'); 
}

echo "\n";
echo "Balance Qty ". $qty . "\n";

输出

Qty 6 | Amt 0 
--------
Qty 1 | Amt 0 
--------
Qty 0 | Amt 9 
--------

Balance Qty 0

请参阅https://eval.in/920847 了解输出。

您可以尝试以不同的数量运行相同的代码。

这是您的用例的粗略逻辑,它将按预期工作。仍然可能有更好的方法以最佳方式做到这一点。

如果能帮到你很高兴。

【讨论】:

  • @LornTitya 你碰巧尝试过吗?
  • RamC,非常感谢。
  • 好吧,如果它对您有帮助,您可以投票赞成我的回答以感谢您的努力。谢谢。
  • 如果有人用 pid=321 和 qty = 7 和 pid=566 和 5 下订单。我该怎么办?请帮忙,谢谢。
  • 您可以在循环中使用相同的代码并执行您提到的两个事务
猜你喜欢
  • 2014-12-16
  • 1970-01-01
  • 2023-03-20
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多