简体   繁体   English

PHP-Mysql在单列中添加值(如果其他列中的值重复)

[英]PHP - Mysql adding values in single column if value in other columns are duplicates

I'm having an issue figuring a part out while writing the below code. 我在编写下面的代码时发现零件有问题。 I am pulling data from 2 different tables and using an inner join to link a column together, which is working fine. 我正在从2个不同的表中提取数据,并使用内部联接将列链接在一​​起,效果很好。 When getting data from the headkey column, the there can be multiple rows with the same "key" to identify an order and then I need to be looking at the Delivery column to see if the order has the value 1 or -1. headkey列获取数据时,可以有多个具有相同“键”的行来标识订单,然后我需要查看Delivery列以查看订单的值是1还是-1。 I need to be able to add the 1 AND the -1 together, so if an order has a 1 AND a -1 that would equal to 0 which means that the delivery was cancelled. 我需要能够将1和-1相加,因此,如果订单的1和-1等于0,则意味着取消了交货。 But if the order had 1 + (-1) + 1, well that will equal a 1. In that case, that is a delivery. 但是,如果订单具有1 +(-1)+ 1,则等于1。在这种情况下,这就是交货。 Is it possible to add values in the delivery column if the values in the headkey column match? 如果headkey列中的值匹配,是否可以在delivery列中添加值? I was thinking using multidimentional arrays, but I dont fully understand them to be able to implement them. 我当时在考虑使用多维数组,但我不完全了解它们是否能够实现它们。

<?php

$TransactionSql = "
SELECT apcshead.Key
, apcshead.DateInvoiced
, apcshead.InvNum
, apcsitem.Headkey
, apcsitem.ItemID
, apcsitem.Quantity
, apcshead.CustomerCode
, customers.BillTo1
, customers.BillTo2
, customers.City
, customers.PostCode
FROM customers 
INNER JOIN (apcshead INNER JOIN apcsitem ON apcshead.Key = apcsitem.Headkey)
ON customers.CustomerCode = apcshead.CustomerCode";


$rs=odbc_exec($conn,$TransactionSql);
while($row = odbc_fetch_array($rs)) 
 {
$Key = odbc_result($rs,"Key");  
$DateInvoiced = odbc_result($rs,"DateInvoiced");
$InvNum = odbc_result($rs,"InvNum");
$Headkey = odbc_result($rs,"Headkey");
$ItemID = odbc_result($rs,"ItemID");
$Quantity = odbc_result($rs,"Quantity");
$CustomerCode = odbc_result($rs,"CustomerCode");

$DateInvoiced = new DateTime($DateInvoiced);
$DateInvoiced_date = $DateInvoiced->format('m-d-Y');

$DeliverySql = "SELECT Quantity, HeadKey FROM apcsitem WHERE HeadKey=20008";

if ($DateInvoiced_date == $Today)
{
    if ($ItemID == 'Delivery' xor $ItemID == 'DeliveryCharge')
    {
    echo '
        <td class="td" valign="top">' . $Key . '</td>
        <td class="td" valign="top">' . $DateInvoiced_date . '</td>
        <td class="td" valign="top">' . $InvNum . '</td>
        <td class="td" valign="top">' . $Headkey . '</td>
        <td class="td" valign="top">' . $ItemID . '</td>
        <td class="td" valign="top">' . $Quantity . '</td>
        <td class="td" valign="top">' . $CustomerCode . '</td>
        </tr> ';
    }
}

}

?>

Here is a snapshot of the table. 这是表的快照。 表例

Stripped Down Version 精简版

$TransactionSql = "SELECT * FROM apcsitem";

$rs=odbc_exec($conn,$TransactionSql);
while($row = odbc_fetch_array($rs)) 
{
$Key = odbc_result($rs,"HeadKey");  
$ItemID = odbc_result($rs,"ItemID");
$Quantity = odbc_result($rs,"Quantity");

if ($ItemID == 'Delivery' xor $ItemID == 'DeliveryCharge')
{
 echo '
        <td class="td" valign="top">' . $Key . '</td>
        <td class="td" valign="top">' . $ItemID . '</td>
        <td class="td" valign="top">' . $Quantity . '</td>
        </tr> ';
    }
}

Below is an example of a real transaction that was done by an confused employee :) 下面是一个由困惑的员工完成的真实交易的示例:)

在此处输入图片说明

So basically what I need to be able to do is if second column contains delivery or deliveryCharge to filter them out. 因此,基本上我需要做的是第二列是否包含delivery或deliveryCharge来将其过滤掉。 Then if any of them have a duplicate HeadKey (value in first column) to add the values of the last column for only the filtered ones. 然后,如果它们中的任何一个具有重复的HeadKey(第一列中的值),则仅为过滤后的列添加最后一列的值。 AKA, the ones with the same HeadKey. 又名,那些具有相同的HeadKey。 In the image above they have Delivery and DeliveryCharge. 在上图中,他们具有Delivery and DeliveryCharge。 They added them and cancelled them. 他们添加并取消了它们。 If you add the last column based on the group (Delivery) the result is 0 and if you add the last column with the group (DeliveryCharge) you will also get 0. Which in fact means after all that, there was no delivery! 如果您基于组(交付)添加最后一列,则结果为0,如果您将最后一组与组(交付Charge)添加在一起,您也将得到0。实际上,这意味着没有交付!

Don't try to optimize by changing your business logic into a mathematical sum. 不要试图通过将业务逻辑更改为数学和来进行优化。

Something like: 就像是:

$delivered = $order["ready"] === "yes" && $order["charged"] === "yes";
$cancelled = $order["ready"] === "yes" && $order["cancelled"] === "yes";

is much more clear. 更清楚。

I would consider using ENUM for true/false or yes/no fields. 我会考虑将ENUM用于true / false或yes / no字段。 It's much more understandable. 容易理解。 You can have as many states as you want so you don't need the wonky -1 . 您可以根据需要拥有任意多个状态,因此不需要笨拙的-1

Can we start with this... 我们可以从这里开始...

SELECT h.Key
     , h.DateInvoiced
     , h.InvNum
     , i.Headkey
     , i.ItemID
     , i.Quantity
     , h.CustomerCode
     , c.BillTo1
     , c.BillTo2
     , c.City
     , c.PostCode 
  FROM customers c
  JOIN apcshead h
    ON h.CustomerCode = c.CustomerCode 
  JOIN apcsitem i
    ON i.Headkey = h.Key;

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

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