在php中动态获取两个表中的记录

时间:2016-09-29 09:48:12

标签: php mysql

我有两张桌子。一个存储基本信息,而另一个存储事务详细信息。我想从表中获取两条记录。根据下面的查询,我可以获取记录,但是,它没有显示所需的所有事务详细信息。请帮助看看并告诉我我做错了什么或更好的方法。

<tr>
<th>SN</th>
<th nowrap="nowrap">Date of Incidence</th>
<th nowrap="nowrap">Week No</th>
<th>Operator's Name</th>
<th>Location</th>
<th>SKU</th>
<th>RGB/Non-RGB</th>
<th>Bottles/CAN per Case</th>
<th>Bottles Only(Cases)</th>
<th>Bottles Only(Bottles)</th>
<th>Liquids Only(Cases)</th>
<th>Liquids Only(Bottles)</th>
<th>Liquids & Bottles(Cases)</th>
<th>Liquids & Bottles(Bottles)</th>
<th>Non-RGB(CAN & PET-Cases)</th>
<th>Non-RGB(CAN & PET-Pieces)</th>
</tr>
<?php 
$s= mysqli_query($connection,"SELECT distinct(tab_bmaster.opname)as opname,tab_bmaster.incDate,tab_bmaster.weekperiod,tab_bmaster.tBO,tab_bmaster.tLO,tab_bmaster.tLB,tab_bmaster.pcBO,tab_bmaster.pcLO,tab_bmaster.pcLB,tab_bdetails.loc,tab_bdetails.sku,tab_bdetails.rgbnrgb,tab_bdetails.bcpercase,tab_bdetails.description,tab_bdetails.container,tab_bdetails.qty,tab_bdetails.transid FROM tab_bmaster INNER JOIN tab_bdetails ON tab_bmaster.transid=tab_bdetails.transid AND tab_bdetails.loc='$loc' AND tab_bdetails.weekperiod=tab_bmaster.weekperiod AND tab_bmaster.weekperiod='$wk' AND cast(tab_bmaster.incDate as date) BETWEEN '$td1' AND '$td2' GROUP BY tab_bmaster.opname,tab_bmaster.incDate ORDER  BY tab_bmaster.incDate ASC");

$c=0;
while($r = mysqli_fetch_array($s)){ $c++;?>
<tr>
<td><?php echo $c;?></td>
<td><?php echo $r['incDate'];?></td>
<td><?php echo 'Week '.$r['weekperiod'];?></td>
<td nowrap="nowrap"><?php echo $r['opname'];?></td>
<td><?php echo $r['loc'];?></td>
<td><?php echo $r['sku'];?></td>
<td><?php echo $r['rgbnrgb'];?></td>
<td><?php echo $r['bcpercase'];?></td>

    <?php 
    //Bottle only-Cases 
    $sql = mysqli_fetch_array(mysqli_query($connection,"SELECT qty as q1 FROM tab_bdetails WHERE opname='".$r['opname']."' AND loc='".$r['loc']."' AND description='bottle Only' AND container='cases' AND weekperiod='".$r['weekperiod']."'  AND incDate='".$r['incDate']."' AND sku='".$r['sku']."' AND transid = '".$r['transid']."'"));?>
    <td><?php echo  $sql['q1'];?></td>
    <?php 
    //Bottle Only - bottles 
    $sql2 = mysqli_fetch_array(mysqli_query($connection,"SELECT qty as q2 FROM tab_bdetails WHERE opname='".$r['opname']."' AND loc='".$r['loc']."' AND description='bottle Only' AND container='bottles' AND weekperiod='".$r['weekperiod']."' AND incDate='".$r['incDate']."' AND sku='".$r['sku']."'  AND transid = '".$r['transid']."'"));?>

    <td><?php echo $sql2['q2'];?></td>
    <?php 
    //Liquid Only-Cases 
    $sql3 = mysqli_fetch_array(mysqli_query($connection,"SELECT qty as q3 FROM tab_bdetails WHERE opname='".$r['opname']."' AND loc='".$r['loc']."' AND description='liquid Only' AND container='cases' AND weekperiod='".$r['weekperiod']."' AND incDate='".$r['incDate']."' AND sku='".$r['sku']."'  AND transid = '".$r['transid']."'"));?>
    <td><?php echo $sql3['q3'];?></td>
    <?php 
    //Liquid Only- Bottles 
    $sql4 = mysqli_fetch_array(mysqli_query($connection,"SELECT qty as q4 FROM tab_bdetails WHERE opname='".$r['opname']."' AND loc='".$r['loc']."' AND description='liquid Only' AND container='bottles' AND weekperiod='".$r['weekperiod']."' AND incDate='".$r['incDate']."' AND sku='".$r['sku']."'  AND transid = '".$r['transid']."'"));?>
    <td><?php echo $sql4['q4'];?></td>
    <?php  
     //Liquids&bottles- cases
    $sql5 = mysqli_fetch_array(mysqli_query($connection,"SELECT qty as q5 FROM tab_bdetails WHERE opname='".$r['opname']."' AND loc='".$r['loc']."' AND description='liquids&bottles' AND container='cases' AND weekperiod='".$r['weekperiod']."' AND incDate='".$r['incDate']."'AND sku='".$r['sku']."'  AND transid = '".$r['transid']."'"));?>
    <td><?php echo $sql5['q5'];?></td>
    <?php 
    //Liquids&bottles-bottles 
    $sql6 = mysqli_fetch_array(mysqli_query($connection,"SELECT qty as q6 FROM tab_bdetails WHERE opname='".$r['opname']."' AND loc='".$r['loc']."' AND description='liquids&bottles' AND container='bottles' AND weekperiod='".$r['weekperiod']."' AND incDate='".$r['incDate']."'AND sku='".$r['sku']."'  AND transid = '".$r['transid']."'"));?>
    <td><?php echo $sql6['q6'];?></td>
    <?php 
    //can&pet-cases 
    $sql7 = mysqli_fetch_array(mysqli_query($connection,"SELECT qty as q7 FROM tab_bdetails WHERE opname='".$r['opname']."' AND loc='".$r['loc']."' AND description='can&pet' AND container='cases' AND weekperiod='".$r['weekperiod']."' AND incDate='".$r['incDate']."'AND sku='".$r['sku']."'  AND transid = '".$r['transid']."'"));?>
    <td><?php echo $sql7['q7'];?></td>
    <?php 
    //can&pet - pieces 
    $sql8 = mysqli_fetch_array(mysqli_query($connection,"SELECT qty as q8 FROM tab_bdetails WHERE opname='".$r['opname']."' AND loc='".$r['loc']."' AND description='can&pet' AND container='pieces' AND weekperiod='".$r['weekperiod']."' AND incDate='".$r['incDate']."'AND sku='".$r['sku']."'  AND transid = '".$r['transid']."'"));?>
    <td><?php echo $sql8['q8'];?></td>
</tr>
<?php };

}?>

以下是两个表格。仅举一条记录,例如: 表:tab_bdetails enter image description here  table:table_bmaster enter image description here

输出是这个,应该显示更多细节只显示一条记录。协助:

enter image description here

0 个答案:

没有答案