【问题标题】:Multidimensional Associative Array - REMOVE DUPLICATES多维关联数组 - 删除重复项
【发布时间】:2021-05-01 10:16:46
【问题描述】:

我有一个 MySQL 查询,我通过一个 while 循环运行,该循环为我提供了一个多维关联数组。

$query2 = "SELECT DISTINCT 
            soc.rowid as id,
            fc.paye as paid
            FROM invoices fc
            LEFT JOIN customers soc on fc.fk_soc = soc.rowid
            AND fc.paidBy < '" . $date . "'";

$unpaidInv = $db->query($query2);

while($row1 = mysqli_fetch_array($unpaidInv, MYSQLI_ASSOC)){
    $params1[] = $row1;
}
var_dump($params1);

var_dump 结果如下:

array(10) {                                                                                                                                                                                                        
  [0]=>                                                                                                                                                                                                            
  array(2) {                                                                                                                                                                                                       
    ["id"]=>                                                                                                                                                                                                       
    string(2) "19"                                                                                                                                                                                                 
    ["paid"]=>                                                                                                                                                                                                     
    string(1) "1"                                                                                                                                                                                                  
  }                                                                                                                                                                                                                
  [1]=>                                                                                                                                                                                                            
  array(2) {                                                                                                                                                                                                       
    ["id"]=>                                                                                                                                                                                                       
    string(2) "22"                                                                                                                                                                                                 
    ["paid"]=>                                                                                                                                                                                                     
    string(1) "1"                                                                                                                                                                                                  
  }                                                                                                                                                                                                                
  [2]=>                                                                                                                                                                                                            
  array(2) {                                                                                                                                                                                                       
    ["id"]=>                                                                                                                                                                                                       
    string(2) "21"                                                                                                                                                                                                 
    ["paid"]=>                                                                                                                                                                                                     
    string(1) "0"                                                                                                                                                                                                  
  }                                                                                                                                                                                                                
  [3]=>                                                                                                                                                                                                            
  array(2) {                                                                                                                                                                                                       
    ["id"]=>                                                                                                                                                                                                       
    string(2) "23"                                                                                                                                                                                                 
    ["paid"]=>                                                                                                                                                                                                     
    string(1) "1"                                                                                                                                                                                                  
  }                                                                                                                                                                                                                
  [4]=>                                                                                                                                                                                                            
  array(2) {                                                                                                                                                                                                       
    ["id"]=>                                                                                                                                                                                                       
    string(2) "21"                                                                                                                                                                                                 
    ["paid"]=>
    string(1) "1"
  }
  [5]=>
  array(2) {
    ["id"]=>
    string(2) "32"
    ["paid"]=>
    string(1) "1"
  }
  [6]=>
  array(2) {
    ["id"]=>
    string(2) "22"
    ["paid"]=>
    string(1) "0"
  }
  [7]=>
  array(2) {
    ["id"]=>
    string(2) "23"
    ["paid"]=>
    string(1) "0"
  }
  [8]=>
  array(2) {
    ["id"]=>
    string(2) "32"
    ["paid"]=>
    string(1) "0"
  }
  [9]=>
  array(2) {
    ["id"]=>
    NULL
    ["paid"]=>
    string(1) "0"
  }
}
string(120) "

目前存在具有不同“付费”值的相同 id 的数组项。 例如,Array item 2, id->21, paid->0 – Array item 4, id->21, paid->1.

我需要我的数组只返回全为 1 的结果。因此,如果数组有两次 id->21 但付费项目有 paid->1 和 paid->0 它不应该出现在数组中。

只有全为 1 的数组项。

【问题讨论】:

  • SQL 中的WHERE fc.paye = 1 会是最好的方法吗?
  • 否,因为它必须遍历数据集中的所有出现,才能从结果数组中排除同时支付 1 和支付 0 的 id。因此,对于支付 1 和 0 的 id,它应该是错误的,而需要包含多个支付为 1 或仅支付 1 的 id。
  • 是否可以重构数组以达到结果?
  • 请考虑切换到准备好的语句到prevent SQL injection。
  • 是的,可以重构数组。谢谢@El_Vanja。

标签: php mysql arrays multidimensional-array


【解决方案1】:

对于任何对可用解决方案感兴趣的人,请参阅下文。

SELECT DISTINCT
soc.rowid as id
FROM invoices fc
LEFT JOIN customers soc on fc.fk_soc = soc.rowid
WHERE soc.rowid IS NOT NULL
AND fc.paidBy < '" . $date . "'"
AND soc.rowid NOT IN(
SELECT DISTINCT
soc.rowid as id
FROM invoices fc
LEFT JOIN customers soc on fc.fk_soc = soc.rowid
AND fc.paye = '0'
WHERE soc.rowid IS NOT NULL
AND fc.paidBy < '" . $date . "'");

【讨论】:

    【解决方案2】:

    我会在请求中尝试这样的事情:

    SELECT DISTINCT 
            soc.rowid as id,
            fc.paye as paid
            FROM invoices fc
            LEFT JOIN customers soc on fc.fk_soc = soc.rowid
            WHERE fc.paidBy < '" . $date . "'"
            AND soc.rowid NOT IN (SELECT DISTINCT 
            soc2.rowid as id,
            FROM invoices fc2
            LEFT JOIN customers soc2 on fc2.fk_soc = soc2.rowid
            WHERE fc2.paidBy < '" . $date . "'"
            AND fc2.paye = '0');
    

    为了更精确地进行调整,但考虑到这一点,您将只保留不在子查询中的行,该子查询检索值为零的行

    【讨论】:

      猜你喜欢
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 2014-05-26
      • 2012-12-14
      • 2021-02-16
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多