Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中的可用性计算逻辑_Php_Mysql - Fatal编程技术网

php中的可用性计算逻辑

php中的可用性计算逻辑,php,mysql,Php,Mysql,这是一个mysql查询结果。现在,我必须根据每个challan Id计算可用的php数量。预期结果将是: ChallanId | Avaiable Qty | unitCostPrice | totalPrice 11 | 7 | 2 | 14 12 | 10 | 1 | 10 如何在php中实现它?使用foreach

这是一个mysql查询结果。现在,我必须根据每个challan Id计算可用的php数量。预期结果将是:

ChallanId |  Avaiable Qty  |  unitCostPrice  | totalPrice     
  11      |    7           |     2           |   14         
  12      |    10          |     1           |   10

如何在php中实现它?使用foreach或任何其他技巧。

虽然您希望为
PHP
解决方案实现这一点,但在这里,我认为SQL query也可以做到这一点:

select
    ChallanId,
    sum(case when Narration = 'in' then ItemIn when Narration = 'out' then  0 - ItemOut end) as Avaiable_Qty,
    unitCostPrice,
    sum(case when Narration = 'in' then ItemIn when Narration = 'out' then  0 - ItemOut end) * unitCostPrice as totalPrice
from (your query here...)
having Avaiable_Qty > 0
group by ChallanId

虽然您希望为
PHP
解决方案实现这一点,但在这里,我认为SQL query也可以做到这一点:

select
    ChallanId,
    sum(case when Narration = 'in' then ItemIn when Narration = 'out' then  0 - ItemOut end) as Avaiable_Qty,
    unitCostPrice,
    sum(case when Narration = 'in' then ItemIn when Narration = 'out' then  0 - ItemOut end) * unitCostPrice as totalPrice
from (your query here...)
having Avaiable_Qty > 0
group by ChallanId

这是一种psuedo代码,因为您没有与PHP共享您的实现

首先,您可以假设ItemIn或ItemOut的叙述不是零,事实上这样做可能更好,因为您可以在同一行中引入项目in和项目out。但这不是问题所在

$output = array();
foreach ($dbResults as $row) {
    // Create an output entry
    if (!array_key_exists($row['ChallanID'], $output)) {
        $output[$row['ChallanID']] = array('Qty' => 0, 'CostPrice' => 0, 'Total' => 0);
    }

    // Add to totals
    $output[$row['ChallanID']]['Qty'] += ($row['ItemIn'] - $row['ItemOut']);
    $output[$row['ChallanID']]['CostPrice'] = $row['UnitCostPrice']; // you may want to average this instead?
    $output[$row['ChallanID']]['Total'] += $output[$row['ChallanID']]['Qty'] * $output[$row['ChallanID']]['CostPrice'];
}

$output
现在应该包含所需的输出。未经测试。

这是一种psuedo代码,因为您没有与PHP共享您的实现

首先,您可以假设ItemIn或ItemOut的叙述不是零,事实上这样做可能更好,因为您可以在同一行中引入项目in和项目out。但这不是问题所在

$output = array();
foreach ($dbResults as $row) {
    // Create an output entry
    if (!array_key_exists($row['ChallanID'], $output)) {
        $output[$row['ChallanID']] = array('Qty' => 0, 'CostPrice' => 0, 'Total' => 0);
    }

    // Add to totals
    $output[$row['ChallanID']]['Qty'] += ($row['ItemIn'] - $row['ItemOut']);
    $output[$row['ChallanID']]['CostPrice'] = $row['UnitCostPrice']; // you may want to average this instead?
    $output[$row['ChallanID']]['Total'] += $output[$row['ChallanID']]['Qty'] * $output[$row['ChallanID']]['CostPrice'];
}

$output
现在应该包含所需的输出。未经测试。

我会通过更智能的查询在SQL中进行计算

select 
    ChallanId,
    UnitCostPrice,
    sum(`ItemIn`)-sum(`ItemOut`) as AvailableQty,
    (sum(`ItemIn`)-sum(`ItemOut`))*UnitCostPrice as totalPrice
from tbl
group by ChallanId

我会通过使用更智能的查询在SQL中进行计算

select 
    ChallanId,
    UnitCostPrice,
    sum(`ItemIn`)-sum(`ItemOut`) as AvailableQty,
    (sum(`ItemIn`)-sum(`ItemOut`))*UnitCostPrice as totalPrice
from tbl
group by ChallanId

你能告诉我们你试过什么吗?你能告诉我们你试过什么吗?