我想请求您的帮助缩短我的查询,以便在while循环中不会有查询。
这是我的代码:
<?php
if(isset($_POST['cat']))
{
$stmt = $dbConn->prepare("SELECT category, extIdentifierID, identifier, ext_no FROM `push`.`transferlog_extcategory` LEFT JOIN `push`.`transferlog_extidentifier` ON transferlog_extcategory.catID = transferlog_extidentifier.catID WHERE transferlog_extcategory.catID = :catID");
$stmt->execute(':catID'=>$_POST['cat']);
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo '<tr><th colspan=2 style=background-color:#f5f5f5;>'.$row['identifier'].'</th></tr>';
$sql = "SELECT extID extIDMain, extension, transfertype, ext_no, (SELECT COUNT(logID) FROM push.transferlog_agentlog WHERE extID=extIDMain ) logCount FROM push.transferlog_extension WHERE transferlog_extension.extIdentifierID = :extIdentifierID";
$stmt2 = $dbConn->prepare($sql);
$stmt->execute(':extIdentifierID'=>$row['extIdentifierID']);
while($row1 = $stmt2->fetch(PDO::FETCH_ASSOC))
{
echo '<tr><td>'.$row1['extension'].'</td> <td>'.$row1['logCount'].'</td></tr>';
}
}
$dbConn = null;
}
?>
样品输出
的头1
OUTPUT1
OUTPUT2
OUTPUT3
OUTPUT4
OutPut5
的头2
OUTPUT1
OUTPUT2
OUTPUT3
OUTPUT4
OutPut5
的 Header3
OUTPUT1
OUTPUT2
OUTPUT3
OUTPUT4
OutPut5