PHP产生不正确的算术结果

时间:2016-01-27 16:56:09

标签: php mysql

我有一个PHP脚本,它启动一个mysql查询并返回一个带有聚合列的结果集。

我需要对这些列进行进一步的聚合,因此在我的脚本中已经这样做了。

问题在于,对于20个限制中的前7个结果,计算显然是不正确的。

这是否与表大的事实有关? (3.5米行)

我可以在SQL级别完成额外的聚合吗?说A是一个聚合,而B是一个聚合,我可以列C = B / A吗?

Output

//QUERY
        $resultsquery = $db->prepare("SELECT 4_nationality.nationality, COUNT(DISTINCT(txn_id)) as numtrans,
                                        SUM(sales) as sales, SUM(units) as units, YrQtr
                                        FROM 1_txns INNER JOIN 4_nationality USING (nationality_id)
                                        WHERE YrQtr LIKE :period
                                        GROUP BY nationality_id 
                                        ORDER BY numtrans DESC
                                        LIMIT 20");
        $resultsquery->bindParam(":period", $period);
        $resultsquery->execute();

        //BUILD TABLE

        echo "<div class='col-xs-12 col-sm-10'>";
        echo $select;


        echo "<table class='table table-striped'><tr>";
            echo "<th>Nationality</th>";
            echo "<th># Trans</th>";
            echo "<th>Sales</th>";
            echo "<th>Units</th>";
            echo "<th>ATV</th>";
            echo "<th>UPB</th>";
            echo "<th>ARP</th>";
        echo "</tr>";

        while($row2 = $resultsquery->fetch(PDO::FETCH_ASSOC)){

            //FURTHER CALCULATIONS

            $nat = $row2['nationality'];
            $numtrans = number_format($row2['numtrans'], 0);
            $sales = number_format($row2['sales'], 0);
            $units = number_format($row2['units'], 0);
            $atvc = $sales / $numtrans;
            $atv = number_format($atvc, 2);
            $upbc = $units / $numtrans;
            $upb = number_format($upbc, 1);
            $arpc = $sales / $units;
            $arp = number_format($arpc, 2);

                //DISPLAY
                echo "<tr>";
                    echo "<td>".$nat."</td>";
                    echo "<td>".$numtrans."</td>";
                    echo "<td>".$sales."</td>";
                    echo "<td>".$units."</td>";
                    echo "<td>".$atv."</td>";
                    echo "<td>".$upb."</td>";
                    echo "<td>".$arp."</td>";
                echo "</tr>";

        }

        echo "</table>";
        echo "</div>";  

编辑:非常感谢这两个答案,很难选择接受哪个,因为他们都以不同的方式帮助了我。但我决定接受答案,提供有关问题标题的更多细节。

2 个答案:

答案 0 :(得分:3)

这可能是因为您正在对格式化字符串执行算术运算。

尝试:

$nat = $row2['nationality'];
$numtrans = number_format($row2['numtrans'], 0);
$sales = number_format($row2['sales'], 0);
$units = number_format($row2['units'], 0);
$atvc = $row2['sales'] / $row2['numtrans'];
$atv = number_format($atvc, 2);
$upbc = $row2['units'] / $row2['numtrans'];
$upb = number_format($upbc, 1);
$arpc = $row2['sales'] / $row2['units'];
$arp = number_format($arpc, 2);

编辑:

实际上我确定这是发生的事情:

echo '15,263,316' / '568,393'; // 0.026408450704225
echo 15263316 / 568393;        //26.853455267746

答案 1 :(得分:2)

它无效,因为您使用27,002,864而不是27002864等格式化数字进行计算。 是的,它在SQL中更容易:

SELECT 4_nationality.nationality, COUNT(DISTINCT(txn_id)) as numtrans,
  SUM(sales) as sales, SUM(units) as units, YrQtr, 
  sum(sales)/count(distinct txn_id) atvc, 
  sum(units)/count(distinct txn_id) upbc, 
  sum(sales)/sum(units) arp
FROM 1_txns INNER JOIN 4_nationality USING (nationality_id)
WHERE YrQtr LIKE :period
GROUP BY nationality_id 
ORDER BY numtrans DESC
LIMIT 20