我目前正试图了解如何使用联接表中的SQL结果在现有循环中循环结果。
首先,这是我当前的代码:
<?php
include('OrderCore/connect-db.php');
$POIds = array();
if ($result = $mysqli->query("SELECT ProductionOrderID FROM ProductionOrder" ) ) {
while ($row = $result->fetch_object()) {
$POIds[] = $row->ProductionOrderID;
}
}
foreach ( $POIds as $index => $OrderId ) {
if ( $result = $mysqli->query("
SELECT *
FROM ProductionOrder AS p
LEFT JOIN ProductionOrderStatus AS s ON ( p.ProductionOrderID = s.ProductionOrderStatusID )
LEFT JOIN NotGood AS n ON ( p.ProductionOrderID = n.NGID )
LEFT JOIN BatchOrder AS b ON ( p.ProductionOrderID = b.ProductionOrderID)
LEFT JOIN Brand AS bd ON ( p.ProductionOrderID = bd.BrandID )
LEFT JOIN CustomerOrder AS co ON ( p.ProductionOrderID = co.COID )
LEFT JOIN Customer AS c ON ( p.ProductionOrderID = c.CustomerID )
LEFT JOIN CustomerOrderStatus AS cos ON ( p.ProductionOrderID = cos.COStatusID )
WHERE p.ProductionOrderID='$OrderId'") ) {
while( $row = $result->fetch_object() ) {
print "<h1>Order: $OrderId</h1>";
print "<table class='table table-striped'>";
print "<tr> <th>PO ID</th> <th>PO #</th> <th>Order Quantity</th> <th>Balance Left</th> <th>Production Date</th> <th>Production Order Status</th> <th>Not Good ID</th> </tr>";
print "<td>" . $row->ProductionOrderID . "</td>";
print "<td>" . $row->PONum . "</td>";
print "<td>" . $row->OrderQTY . "</td>";
print "<td>" . $row->BalLeftNum . "</td>";
print "<td>" . $row->ProductionDate . "</td>";
print "<td>" . $row->ProductionOrderStatusID . "</td>";
print "<td>" . $row->NGID . "</td>";
print "</tr>";
print "</table>";
//BatchOrder
print "<table class='table table-striped'>";
print "<tr> <th>Batch ID</th> <th>Brand Name</th> <th>Batch Quantity</th> <th>Availability Date</th> <th>Remaining Balance</th> <th>Production Order ID</th> </tr>";
print "<td>" . $row->BatchID . "</td>";
print "<td>" . $row->BrandID . "</td>";
print "<td>" . $row->BatchQTY . "</td>";
print "<td>" . $row->AvailDate . "</td>";
print "<td>" . $row->RemainBal . "</td>";
print "<td>" . $row->ProductionOrderID . "</td>";
print "</tr>";
print "</table>";
//CustomerOrder
print "<table class='table table-striped'>";
print "<tr> <th>Customer ID</th> <th>Customer Name</th> <th>Invoice Quantity</th> <th>Invoice #</th> <th>Shipping Date</th> <th>Batch ID</th> <th>CO Status</th> </tr>";
print "<td>" . $row->COID . "</td>";
print "<td>" . $row->CustomerID . "</td>";
print "<td>" . $row->InvoiceQTY . "</td>";
print "<td>" . $row->InvoiceNum . "</td>";
print "<td>" . $row->ShipDate . "</td>";
print "<td>" . $row->BatchID . "</td>";
print "<td>" . $row->COStatusID . "</td>";
print "</tr>";
print "</table>";
}
}
else
{
print "No results to display!";
}
}
$mysqli->close();
?>
该代码当前产生以下结果:https://i.imgur.com/y7uh6nk.png。
这几乎是正确的预期行为...每个
ProductionOrderID
应生成一个新表,该表包含2个子表:BatchOrder
和CustomerOrder
。但目前每个子表只显示1个结果。因此需要澄清的是,用户可以创建任意数量的
ProductionOrder
(因此foreach
遍历整个数组)。每个ProductionOrder
可以包含:零个,一个或许多BatchOrder
,每个BatchOrder
可以包含:零个,一个或许多CustomerOrder
。当前问题:
根据上方的屏幕快照链接,它仅显示1个
BatchOrder
和每个CustomerOrder
1个ProductionOrder
。我的示例数据包含ProductionOrderID=1
的多个批处理订单,但未显示。我不确定这个问题是否部分与PHP和SQL有关。我是这两种语言的新手,但我怀疑不正确。但是,这是唯一(当前)正确显示每个
LEFT JOIN
以及每个ProductionOrder
和BO
的方法...并不是全部。我也怀疑我需要在现有的while循环中执行另一个循环,但是由于当前的尝试均未成功,因此我不确定采用正确的方法。详细资料
这是我的数据库SQL的副本,其中包含示例数据:https://pastebin.com/A3rt8kX4
还有我的ERD以显示预期的行为:https://i.imgur.com/idVR5ev.png
我们非常感谢您提供的不仅帮助解决当前问题的帮助,而且还帮助我了解了为什么会出错。
编辑:1
我已经修复了SELECT语句中加入的键,但是现在只能看到一个PO:
https://i.imgur.com/doRmS0c.png
SELECT *
FROM ProductionOrder AS p
INNER JOIN ProductionOrderStatus AS s ON ( p.ProductionOrderID = s.ProductionOrderID )
INNER JOIN NotGood AS n ON ( p.ProductionOrderID = n.ProductionOrderID )
INNER JOIN BatchOrder AS b ON ( p.ProductionOrderID = b.ProductionOrderID )
INNER JOIN Brand AS bd ON ( b.BatchID = bd.BatchID )
INNER JOIN CustomerOrder AS co ON ( b.BatchID = co.BatchID )
INNER JOIN Customer AS c ON ( co.COID = c.COID )
INNER JOIN CustomerOrderStatus AS cos ON ( co.COID = cos.COID )
WHERE p.ProductionOrderID='$OrderId'") ) {
最佳答案
您需要将主键连接到相应的外键,例如:
LEFT JOIN ProductionOrderStatus AS s ON ( p.ProductionOrderID = s.ProductionOrderStatusID )
应该
LEFT JOIN ProductionOrderStatus AS s ON ( p.ProductionOrderID = s.ProductionOrderID )
大多数联接都存在相同的问题。