我目前正试图理解如何使用连接表的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
s。
当前问题:
根据上面的屏幕截图链接,它每BatchOrder
只显示1 CustomerOrder
和1 ProductionOrder
。我的示例数据包含ProductionOrderID=1
的多个批量订单,但它们不会显示。
我不确定问题是部分PHP还是部分SQL相关。我是两种语言的新手,但我怀疑LEFT JOIN
是不正确的。但是,它是唯一一种(当前)正确显示每个ProductionOrder
并且每个BO
和CO
的方法...只是不是全部。我还怀疑我需要在现有的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'") ) {
答案 0 :(得分:1)
您需要将主键加入相应的外键,例如:
LEFT JOIN ProductionOrderStatus AS s ON ( p.ProductionOrderID = s.ProductionOrderStatusID )
应该是
LEFT JOIN ProductionOrderStatus AS s ON ( p.ProductionOrderID = s.ProductionOrderID )
您在大多数联接中遇到同样的问题。