SQL QUERY IN PHP,我可以将它们组合起来吗?

时间:2018-02-12 15:28:14

标签: javascript php sql

我的PHP中有一个SQL查询,它返回一个包含尚未完成的产品的表。但是对于要执行的查询,我必须手动输入onrow数字以使查询执行。但我有第二个查询,它返回了明天交货日期的产品的所有结果。那么如何进行第一次查询以从第二个查询中获取onrow数字以获取第二个查询的结果,并使用未完成的明天产品数据打印该表。

以下是php中第一个查询的代码:

<?php
$otsitav=$_POST['tid'];
$qryType=$_POST['qryType'];
$db=new mysqli('192.168.1.35','root','','newpms');
if($qryType=="works"){
$msc=microtime(true);
$result=$db->query($query);
$num_results=$result->num_rows;
if($num_results>0){
echo '<table border="1" class="db-table">
<thead>
<tr>
<th width="16%">Aeg</th>
<th width="6%">ID</th>
<th width="8%">Liin</th>
<th width="47%">Toode</th>
<th width="15%">Alus</th>
<th width="8%">Arv</th>
<th width="8%">Töötaja</th>
</thead>
<tbody>';
for ($i=0; $i <$num_results; $i++){
$row = $result->fetch_assoc();
echo '<tr>
<td>'.$row['timestamp'].'</td>
<td>'.$row['idnr'].'</td>
<td>'.$row['prLine'].'</td>
<td>'.utf8_encode($row['glass']).'</td>
<td>'.$row['rack'].'</td>
<td>'.$row['qty'].'</td>
<td>'.utf8_encode($row['fullName']).'</td>
</tr>';
}
echo "</tbody></table>";}

else{
    echo "<h2>Puudub info sellise toote kohta</h2>";
    }
$msc=microtime(true)-$msc;
echo "Kestus:".round($msc,2)." sec";
$result->free();
}
if($qryType=="rowStat"){
$msc=microtime(true);
$query="select proddb.onrrnr,proddb.idnr,proddb.onr,proddb.prod,proddb.qty from proddb WHERE proddb.onr=".$otsitav." AND proddb.idnr<>'0'";
$result=$db->query($query);
$num_results=$result->num_rows;
if($num_results>0){
    echo '<table border="1" class="db-table">
<thead>
<tr>
<th width="">Tellimus</th>
<th width="6">ID</th>
<th width="8">Toode</th>
<th width="">Valmis</th>
</thead>
<tbody>';
    for ($i=0; $i<$num_results; $i++){
        $row = $result->fetch_assoc();
        $query1="SELECT p1.onrrnr, p1.prLineSh, p1.glLiteNr, p1.seqNr, p1.planQty FROM pf_prodQtySeq p1 INNER JOIN (SELECT onrrnr, glLiteNr, MAX(seqNr) AS seqNr FROM pf_prodQtySeq  where onrrnr=".$row['onrrnr']." GROUP BY  glLiteNr) p2 ON p2.glLiteNr = p1.glLiteNr AND p2.seqNr = p1.seqNr AND p2.onrrnr=p1.onrrnr";
        $result1=$db->query($query1);
          $qtyMultiplier=1;
        $num_results1=$result1->num_rows; 
        if($num_results1>0){
            $liteOrdQty=0;
            $remQty=0;
            for ($i1=0; $i1 <$num_results1; $i1++){ 
                if($num_results1>1){
                 $qtyMultiplier=$num_results1;
            }
                $row1 = $result1->fetch_assoc();
                $liteOrdQty=$liteOrdQty+$row['qty'];
                $remQty=$remQty+$row1['planQty'];
            }
            $liteOrdQty=$liteOrdQty/$qtyMultiplier;
            $remQty=$remQty/$qtyMultiplier;
            $doneQty=($liteOrdQty-$remQty);
        }
        if($liteOrdQty===$remQty){
            echo '<tr style="font-weight:bold">
        <td>'.$row['onr'].'</td>
        <td>'.$row['idnr'].'</td>
        <td>'.utf8_encode($row['prod']).'</td>
        <td style = "color:red">'.$liteOrdQty."/".$doneQty.'</td>
        </tr>';
        }
    }
}
}
$db->close();
?>

第二个女巫让我明天约会送货产品:

$sql = "SELECT CURDATE(),`onrrnr`, `deldate`, `cusref1` \n"
    . "FROM `proddb`\n"
    . "WHERE `deldate` = CURDATE()+1\n"
    . "ORDER BY `proddb`.`deldate` ASC";

我可以帮助他们如何工作并将它们结合起来吗? 谢谢!

Shema for third query

Shema for second and first query

1 个答案:

答案 0 :(得分:0)

您可以在PHP中执行多个mysql查询,如下所示:

$sql= "SELECT p1.onrrnr, p1.prLineSh, p1.glLiteNr, p1.seqNr, p1.planQty FROM pf_prodQtySeq p1 INNER JOIN (SELECT onrrnr, glLiteNr, MAX(seqNr) AS seqNr FROM pf_prodQtySeq  where onrrnr=".$row['onrrnr']." GROUP BY  glLiteNr) p2 ON p2.glLiteNr = p1.glLiteNr AND p2.seqNr = p1.seqNr AND p2.onrrnr=p1.onrrnr;";
$sql.= "SELECT CURDATE(),`onrrnr`, `deldate`, `cusref1` "FROM `proddb` WHERE `deldate` = CURDATE()+1 "ORDER BY `proddb`.`deldate` ASC;";