简体   繁体   English

从两个不同的数据库查询中找到两个计数列之间的差异,并在PHP中显示结果

[英]Find the difference between two count columns from two different database queries and display result in PHP

I asked a question last week and had assumed that the final part of it would be the straight forward part. 上周我问了一个问题,并假设其中的最后一部分将是直截了当的部分。 However that is not proving to be the case. 但是事实并非如此。 Here is a link to the original question and background. 这是原始问题和背景的链接

I have two databases so have done 2 SQL queries and have merged the results into a table with four columns that displays 我有两个数据库,所以已经完成了2个SQL查询,并将结果合并到一个具有四列的表中

Name | 姓名| Number| 号码| db1 Count | db1计数| db2 Count | db2计数|

That displays all the information perfectly. 这样可以完美显示所有信息。 However, I need to get it to display, in a 5th column, the difference between db1 Count and db2 Count. 但是,我需要在第5列中显示db1 Count和db2 Count之间的差异。 This is the code I have: 这是我的代码:

$results = array();
while($row = $result1->fetch_assoc()) {
 //Adding all the 1st query results 
    $results[$row['number']] = $row;
}

while($row = $result2->fetch_assoc()) {
    if(! isset($results[$row['number']]) {
          $results[$row['number']] = $row;
    }else {
      $results[$row['number']]['db2 Count'] = $row['db2 Count'];
    }         
}
$db1_count = $row['db1 Count'];
$db2_count = $row['db2 Count'];
$difference = ($db1_count - $db2_count);


if ($results) {
    echo"<TABLE><caption>Total Call Count Overview</caption><TR>
      <TH>Number</TH>
      <TH>Company</TH>
      <TH>db1 Count</TH>
      <TH>db2 Count</TH>
      <TH>Difference</TH></TR>";

    foreach($results as $row) {
         echo"<TR><TD>". $row["number"]. "</TD>";
         echo"<TD>". $row["company"]. "</TD>";
         echo"<TD>". $row["db1 Count"]. "</TD>";
         echo"<TD>". $row["db2 Count"]. "</TD>";
         echo"<TD>". $difference . "</TD></TR>";
    }
    echo"</TABLE>";
 } else {
 echo"0 Results";
}

I had thought that putting the counts as variables 我以为把计数当成变量

$db1_count = $row['db1 Count'];
$db2_count = $row['db2 Count'];
$difference = ($db1_count - $db2_count);

Would allow me to subtract them and echo the $difference variable, but I am just getting a column filled with zeros. 可以让我减去它们并回显$ difference变量,但是我只是得到了一个填充有零的列。 I have also tried 我也尝试过

echo"<TD>". $row['db1 Count'] - $row['db2 Count']. "</TD>";

But only to get the all the db2 Count figures across the top of the page. 但这只是为了获得页面顶部的所有db2 Count数字。

Is there something I am missing, because I have tried answers to similar questions online but to no avail. 是否缺少我所需要的东西,因为我已经尝试过在线回答类似问题,但无济于事。 I'm really quite new to this so any tips in the right direction would be much appreciated. 我真的很新,所以任何朝着正确方向的提示将不胜感激。

Move these lines: 移动这些行:

$db1_count = $row['db1 Count'];
$db2_count = $row['db2 Count'];
$difference = ($db1_count - $db2_count);

inside the : foreach($results as $row) 在: foreach($results as $row)

so 所以

foreach($results as $row) {
    $db1_count = $row['db1 Count'];
    $db2_count = $row['db2 Count'];
    $difference = ($db1_count - $db2_count);
    echo"<TR><TD>". $row["number"]. "</TD>";
    echo"<TD>". $row["company"]. "</TD>";
    echo"<TD>". $row["db1 Count"]. "</TD>";
    echo"<TD>". $row["db2 Count"]. "</TD>";
    echo"<TD>". $difference . "</TD></TR>";
}

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

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