【发布时间】:2017-03-09 02:02:00
【问题描述】:
我需要从两个表中提取列数据,对数据运行计算并将结果保存为别名,然后将这些结果汇总到其他别名中以显示在 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";
此代码运行良好,并按 invoices.id 对所有数据进行分组。但是,项目需求进行了调整,现在所有内容都必须按 invoices.company 分组。当我尝试按 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'] ." ". "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>";
感谢所有帮助。
谢谢,
【问题讨论】:
-
当你执行第一个查询时出现什么错误?
-
好悲痛。不要存储'$'。并查看meta.stackoverflow.com/questions/333952/…
-
大声笑我的错...拼写错误。当我调用 SUM(x.sgtotalquantity) 时,我将其拼写为 x.sgtotalqauntity。修复错误后,我的表格会显示数据,但仍显着偏离。仍然好像 SELECT 语句无法找到每个发票行来汇总派生表中的这些值......
-
其实,刚刚知道了。格式问题。我将发布更改。
-
@Strawberry 请解释为什么不应该存储“$”。放置 cmets/answers 时,解释是关键...
标签: php mysql derived-table