由于子查询,来自 MySQL 结果的 PHP 数组具有重复值


PHP array from MySQL result has duplicate values due to subquery

我使用MySQL查询将结果数组导入PHP。但是,查询使用子查询,可能会导致子查询值在数组中显示两次,这是不正确的。我花了很长时间查看 SQL,看看我是否可以阻止它,但根本没有成功。我现在想知道修改数组以纠正此错误是否更容易。

数组:

    array(436) {
  [0]=>
  array(8) {
    ["id"]=>
    string(2) "75"
    ["subtotal"]=>
    string(8) "168.0000"
    ["invoice_type"]=>
    string(1) "1"
    ["adjustment"]=>
    string(4) "0.00"
    ["totalcost"]=>
    string(8) "168.0000"
  }
  [1]=>
  array(8) {
    ["id"]=>
    string(2) "82"
    ["subtotal"]=>
    string(7) "84.0000"
    ["invoice_type"]=>
    string(1) "1"
    ["adjustment"]=>
    string(4) "0.00"
    ["totalcost"]=>
    string(7) "84.0000"
  }
  [2]=>
  array(8) {
    ["id"]=>
    string(2) "86"
    ["subtotal"]=>
    string(8) "224.0000"
    ["invoice_type"]=>
    string(1) "1"
    ["adjustment"]=>
    string(4) "0.00"
    ["totalcost"]=>
    string(8) "224.0000"
  }
  [3]=>
  array(8) {
    ["id"]=>
    string(2) "95"
    ["subtotal"]=>
    string(7) "70.0000"
    ["invoice_type"]=>
    string(1) "1"
    ["adjustment"]=>
    string(4) "9.00"
    ["totalcost"]=>
    string(7) "70.0000"
  }
  [4]=>
  array(8) {
    ["id"]=>
    string(2) "95"
    ["subtotal"]=>
    string(7) "84.0000"
    ["invoice_type"]=>
    string(1) "2"
    ["adjustment"]=>
    string(4) "9.00"
    ["totalcost"]=>
    string(7) "84.0000"
  }

问题:

在 SQL 中,结果按"invoice_type"和"id"分组。SQL 使用子查询来获取"调整"的值。如果结果有"invoice_type = 1"和"invoice_type = 2",则这两个项目都会添加到数组中,但调整值也会添加到我不希望的两个项目中,因为它会加倍。

有没有一种简单的方法可以查看"id"是否重复,如果是,则将其中一个"调整"值设置为 0.00?

如果您认为我真的应该在SQL中涵盖这一点,我也很乐意添加有关该部分的更多信息。

我已经盯着这个太久了,看不到任何其他选择;-(

编辑 - SQL:

SELECT tbl_client.id, tbl_client.first_name, tbl_client.surname, tbl_schedule.invoice_type, sum( duration ) AS totalhours, sum( duration * rate ) AS subtotal, ifnull( adjustment.totaladjustment, 0 ) AS adjustment, (
sum( duration * rate ) + ifnull( adjustment.totaladjustment, 0 )
) AS totalcost
FROM `tbl_schedule`
INNER JOIN tbl_client ON tbl_client.id = tbl_schedule.client
LEFT JOIN (
    SELECT client, sum( amount ) AS totaladjustment
    FROM tbl_invoice_adjustment
    WHERE ( tbl_invoice_adjustment.`date` BETWEEN 1357002061 AND 1359676740 )
    GROUP BY client
    ) adjustment ON adjustment.client = tbl_schedule.client
WHERE tbl_schedule.franchise =1
AND ( STATUS =1 OR `status` =2 OR `status` =4 )
AND `date` BETWEEN 1357002061 AND 1359676740
GROUP BY tbl_schedule.invoice_type, tbl_schedule.client
ORDER BY tbl_client.surname ASC

编辑 - 表结构

tbl_client

编号
特权
标题

first_name

tbl_schedule

编号
特权
日期
客户

invoice_type期间

地位

tbl_invoice_adjustment

编号
特权
客户
日期
描述

这个想法是,查询应该提取给定时间段的调整,并将它们与计划值组合在一起。在我开始实施这种调整之前,一切都很好。它是 99.7% OK,除了在单个时间段内给定客户端有多个"invoice_type"条目的极少数情况。

编辑: - SQL小提琴

这是问题的SQL小提琴。

如果运行该语句,您将看到客户端 ID 5 被赋予了两次调整,尽管数据库中只有一次针对客户端的调整。主查询显示两行,因为分组是按invoice_type进行的。每个结果行运行子查询并获取调整。

小提琴显示所需的输出,但语句未以任何方式固定。

我使用 PHP 方法修复了这个问题,并在必要时运行数组进行更改。非常适合我的需求。

 $showerror = false;
    $lineid = array();
    foreach ($empresults as $invoiceline => $line) {
        if (isset($lineid[$line['id']])) {
            $empresults[$invoiceline]['duplicate'] = true;
            $empresults[$lineid[$line['id']]]['duplicate'] = true;
            if ($empresults[$lineid[$line['id']]]['adjustment'] != 0) {
                $empresults[$lineid[$line['id']]]['totalcost'] = $empresults[$lineid[$line['id']]]['totalcost'] - $empresults[$lineid[$line['id']]]['adjustment'];
                $empresults[$lineid[$line['id']]]['adjustment'] = 0;
            }
            $showerror = true;
        } else {
            $empresults[$invoiceline]['duplicate'] = false;
        }
        $lineid[$line['id']] = $invoiceline;
    }