简体   繁体   English

如果多个键是相同的MyPHP,则对PHP求和列值

[英]PHP Sum column values if multiple keys are the same MySql

I have following table which results are fetched from mysql query. 我有下表,其结果是从mysql查询中获取的。 These values are for example only. 这些值仅作为示例。

Item | Location |  CheckIn   |  Checkout  | Quantity

  A  |   abc    | 10/10/2015 |            |    20
  A  |   abc    |            | 11/10/2015 |    10
  B  |   def    | 11/10/2015 |            |    25
  A  |   abc    | 12/10/2015 |            |    5
  B  |   def    |            | 13/10/2015 |    10
  B  |   def    | 14/10/2015 |            |    3
  A  |   ghi    | 14/10/2015 |            |    30

I want to total up the quantity based on same item and location, and with condition. 我想根据相同的项目和位置以及条件总计数量。 For example, if item check in, quantity of the item is added up into the location. 例如,如果检入项目,则将项目数量加到该地点。 If item check out, quantity of the item is subtracted from total current quantity of item at specific location. 如果检出项目,则从特定位置的当前总项目数量中减去项目数量。

I have second table which will display each item at specific location with total quantity of the item at that location. 我有第二张表,该表将显示特定位置的每个项目以及该位置的项目总数。 What I have so far is something like this: 到目前为止,我有这样的事情:

Item | Location | Total Quantity

  A  |   abc    |  20
  A  |   abc    |  10
  B  |   def    |  35
  A  |   abc    |  40
  B  |   def    |  30
  B  |   def    |  33
  A  |   ghi    |  63

The expected result should be like this: 预期结果应如下所示:

Item | Location | Total Quantity

  A  |   abc    |  15
  B  |   def    |  18
  A  |   ghi    |  30

The code I did so far for second table: 到目前为止,我为第二张表所做的代码:

<?php
$result = mysql_query("SELECT * FROM itemloc INNER JOIN tblitem ON itemloc.itemId=tblitem.itemId INNER JOIN tblloc ON itemloc.locId=tblloc.locId");
$loc = array();
$total = 0;

while($row = mysql_fetch_array($result))
{
$checkout = $row['itemLocCheckOut'];
if($loc[$row['locId']] = $row['itemId']){
    if(is_null($checkout)){
        $qty = +$row['itemLocQty'];
        }
    else{
        $qty = -$row['itemLocQty'];
        }
$total+=$qty;
$name = $row['itemNm'];
$locnm = $row['locNm'];
}
?>
<tr>
    <td><?php echo $name;?></td>
    <td><?php echo $locnm;?></td>
    <td><?php echo $total;?></td>
</tr>

<?php
}
?>

Any help would be much appreciated. 任何帮助将非常感激。 Thank you. 谢谢。

I would make an array with first index item string and the second the Location. 我将使用第一个索引项目字符串和第二个位置作为数组。
For each row read, I would do: 对于读取的每一行,我都会这样做:

while($row = mysql_fetch_array($result))
{
    $item = $row['item'];
    $location = $row['location'];
    if(!isset($result[$item][$location]))
        $result[$item][$location] = 0;
    if(is_null($row['itemLocCheckOut']))
        $result[$item][$location] += $row['quantity'];
    else
        $result[$item][$location] -= $row['quantity'];
}

At the end, pass with 2 foreach on the table: 最后,在表上传递2 foreach:

foreach($result as $k1 => $v1)
    foreach($v1 as $k2 => $v2)
         echo "Item $k1 location $k2 has $v2 quantity"

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

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