MySQL的UNION展现在重复数据表

问题描述 投票:0回答:1

这真让我抓狂。我有两个表至极包含发票的数据。我有一个客户谁在每个表一个无偿invoce。即时通讯使用此代码加入来自这两个表中的数据,并得到两个发票行:

$stmt = $db->prepare ("

    SELECT invoice_id, invoice_number, customer_id, paid 
    FROM 
    ( 
      SELECT invoice_id, invoice_number, customer_id, paid FROM tbl_invoices
      UNION ALL 
      SELECT invoice_id, invoice_number, customer_id, paid FROM tbl_invoices_el
    ) 
    tbl_invoices
    WHERE customer_id = $customer_id 
    AND paid = '0'
    GROUP BY invoice_number
");

代码工作在phpMyAdmin细,结果是两行数据。在网页上我使用的引导+数据表显示结果的模式窗口。奇怪:不管我如何努力,第一发票重复两次。正如我所说的代码在phpMyAdmin正常工作之前,将所取结果的var_dump()正显示出两个数组,符合市场预期。什么我做错了吗?更新$output .=""wasn't空。我的错。 @Barmar发现YHE问题。

$stmt->execute();
$all_result = $stmt->fetchAll();
$total_rows = $stmt->rowCount();
if ( $total_rows > 0 )  
{
   foreach ($all_result as $row )  
     { 
     /* $output .=" ........... removed */
      echo " 
      <tr style='font-size:13px;'>
        <td width='5%'>
            <div class='form-check' style='margin-bottom:0px'>
                <label class='form-check-label'>
                  <input data-attr='" .$row['invoice_number']. "' id='" .$row['invoice_id']. "' class='form-check-input' type='checkbox' value='" .$customer_id. "'> 
                  <span class='form-check-sign'>
                    <span class='check'></span>
                  </span>
                </label>
            </div>
        </td>
        <td width='15%'>".$row['invoice_number']."</td>
      </tr>   
    ";
    /* echo $output; ........... removed */
  }
}

The result of var_dump()

mysql datatables duplicates union rows
1个回答
1
投票

有没有办法,你可以得到重复的发票号,当你有GROUP BY invoice_number,让你表现出的代码不能产生重复。

我怀疑的问题是,你没有明确$output,而且也与第一发票号码以前的一些输出。放:

$output = "";

foreach循环。

© www.soinside.com 2019 - 2024. All rights reserved.