gpt4 book ai didi

php - 从派生表中提取列并将它们汇总到一个 MySQL SELECT 语句中

转载 作者:行者123 更新时间:2023-11-29 20:08:14 25 4
gpt4 key购买 nike

我需要从两个表中提取列数据,对数据运行计算,并将结果保存为别名,然后将这些结果求和到其他别名中以显示在 php 表中。我试图通过在 SELECT 语句中创建派生表来实现此目的,但它不起作用。我没有收到任何错误,但我的表格仅显示列标题。

代码:

$sql = "SELECT x.company, x.stagestatus, x.shippeddate, SUM(x.totprice) as totalprice, SUM(x.sgtotquantity) as sgtotqty, SUM(x.sgtotalsqft) as sgtotsqft, SUM(x.avgsqftrev) as avgsqftrevenue, SUM(x.avgunitrev) as avgunitrevenue FROM (SELECT t1.company, t1.stagestatus, t1.shippeddate, t1.id, FORMAT(TRIM(LEADING '$' FROM t1.totalprice), 2) AS totprice, t2.invoiceid, SUM(t2.quantity) AS sgtotqauntity, FORMAT(SUM(t2.width * t2.height * t2.quantity ) /144, 2) AS sgtotalsqft, FORMAT((TRIM(LEADING '$' FROM t1.totalprice)/(SUM(t2.width * t2.height * t2.quantity ) /144)), 2) as avgsqftrev, FORMAT((TRIM(LEADING '$' FROM t1.totalprice) / SUM(t2.quantity)), 2) AS avgunitrev
FROM invoices AS t1 INNER JOIN lineitems AS t2 ON t1.id = t2.invoiceid
WHERE (t2.invoiceid = t1.id)
GROUP BY t1.id) x
WHERE x.stagestatus='Complete'
GROUP BY x.company ASC";

这段代码中断了,但是当我单独使用较小的部分时,它工作正常。

例如:

$sql="SELECT invoices.id, invoices.orderdate, invoices.stagestatus, FORMAT(TRIM(LEADING '$' FROM invoices.totalprice), 2) AS totalprice, clients.company, lineitems.invoiceid, SUM(lineitems.quantity) AS sgtotqty, FORMAT(SUM(lineitems.width * lineitems.height * lineitems.quantity ) /144, 2) AS sgtotsqft, FORMAT((TRIM(LEADING '$' FROM invoices.totalprice)/(SUM(lineitems.width * lineitems.height * lineitems.quantity ) /144)), 2) as avgsqftrevenue, FORMAT((TRIM(LEADING '$' FROM invoices.totalprice) / SUM(lineitems.quantity)), 2) AS avgunitrevenue
FROM clients
INNER JOIN invoices ON clients.id = invoices.clientid
INNER JOIN lineitems ON invoices.id = lineitems.invoiceid
WHERE (lineitems.invoiceid = invoices.id) AND invoices.orderdate BETWEEN '".$revenuefrom."' AND '".$revenueto."' AND invoices.stagestatus IN (". implode(',', array_map(function($item) {return '"' . $item . '"'; }, $revenue_check)) .")
GROUP BY invoices.id DESC";

此代码运行良好,并按发票.id 对所有数据进行分组。但是,项目需求进行了调整,现在所有内容都必须按发票.公司分组。当我只是尝试按invoices.company而不是invoices.id进行分组时,我的表完成了,但每个公司行的值非常不准确(sum()ing不正确)。

构建表的 PHP 代码:

$result = $conn->query($sql);


echo "<table id='revenueReportA' align='center' class='report_DT'>
<thead>
<tr>

<th>Customer</th>
<th>Total Revenue</th>
<th>Total SQ FT</th>
<th>AVG Revenue Per SQ FT</th>
<th>Total Number of Units</th>
<th>AVG Revenue Per Unit</th>
</tr>
</head>";


if ($result = $conn->query($sql)) {

// fetch associative array
while ($row = $result->fetch_assoc()) {

echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" ."$". $row['totalprice'] . "</td>";
echo "<td>" . $row['sgtotsqft'] ."&nbsp;&nbsp;". "ft<sup>2</sup>". "</td>";
echo "<td>" ."$". $row['avgsqftrevenue'] . "</td>";
echo "<td>" . $row['sgtotqty'] . "</td>";
echo "<td>" ."$". $row['avgunitrevenue'] . "</td>";
echo "</tr>";
echo "</tbody>";
}

echo "</table>";

echo "<BR>";

感谢所有帮助。

谢谢,

最佳答案

我有拼写错误和格式问题。通过格式化最终数据而不是在嵌入的 SELECT 语句中格式化,我的表数据是准确的。

成功代码:

$sql = "SELECT x.company, x.stagestatus, x.shippeddate, FORMAT(SUM(x.totprice), 2) as totalprice, FORMAT(SUM(x.sgtotquantity), 2) as sgtotqty, FORMAT(SUM(x.sgtotalsqft), 2) as sgtotsqft, FORMAT(SUM(x.avgsqftrev), 2) as avgsqftrevenue, FORMAT(SUM(x.avgunitrev), 2) as avgunitrevenue FROM (SELECT t1.company, t1.stagestatus, t1.shippeddate, t1.id, TRIM(LEADING '$' FROM t1.totalprice) AS totprice, t2.invoiceid, SUM(t2.quantity) AS sgtotquantity, SUM(t2.width * t2.height * t2.quantity ) /144 AS sgtotalsqft, (TRIM(LEADING '$' FROM t1.totalprice)/(SUM(t2.width * t2.height * t2.quantity ) /144)) as avgsqftrev, (TRIM(LEADING '$' FROM t1.totalprice) / SUM(t2.quantity)) AS avgunitrev
FROM invoices AS t1 INNER JOIN lineitems AS t2 ON t1.id = t2.invoiceid
WHERE (t2.invoiceid = t1.id)
GROUP BY t1.id) x
WHERE x.stagestatus='Complete'
GROUP BY x.company ASC";

谢谢!!!

关于php - 从派生表中提取列并将它们汇总到一个 MySQL SELECT 语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40266767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com