总结查询结果

时间:2013-05-22 02:44:50

标签: mysql sum

我有reservationpostflight表。我必须在reservation表中进行查询,并在postflight表中查找数据。

$query="select * from reservation where date(fdate) between '$datefrom' and '$dateto' and status in ('Flown') $aircraft order by $sort";
$result=mysql_query($query) or die(mysql_error());  
echo "<div class='box'><table class='hovertable'>
    <th>Flight Date</th> 
    <th>Client Name</th> 
    <th>Group Code</th>
    <th>Aircraft</th>
    <th>Block Time</th>
    <th>Waiting Time</th>
    <th>Charter Fee</th>
    <th>Take-off and Landing Fee</th>
    <th>Waiting Time Fee</th>
    <th>Other Charges</th>
    <th>Sub-Total</th>
    <th>Value-added Tax</th>
    <th>Total Service Invoice Amount</th>
    <th>Reservation No.</th>
    </tr>";

while($row=mysql_fetch_array($result))
{
    $rvno=$row['reservno'];
    $yr=(string) date("Y");
    $rn=$yr."-".$rvno;
    $a=mysql_query("select *, (fdf + fce + aef + hf + sfp) as 'tcharge' from postflight where reservno='$rvno'") or die(mysql_error());
    //$e=mysql_fetch_array($a);
    while($b=mysql_fetch_array($a))
    {
    echo"<tr><td>".$row['fdate']."</td><td>".$b['cliename']."</td><td>".$row['grpcode']."</td><td>".$row['acode']."</td><td>".$row['btime']."</td><td>".$b['wtime']."</td><td>".$b['total_cfee']."</td><td>".$b['total_tol']."</td><td>".$b['total_wtfee']."</td><td>".$b['tcharge']."</td><td>".$b['sub_total']."</td><td>".$b['vat']."</td><td>".$b['total_service_invoice_amt']."</td><td>".$rn."</td></tr>";
    }

}

在我关闭</table>之前,我想添加另一行,根据上面查询的输出数据,总结所有必需的字段。像,

echo "<tr><td colspan='6'></td><td><b>sum</b></td><td><b>sum</b></td><td><b>sum</b></td><td><b>sum</b></td><td><b>sum</b></td><td><b>sum/b></td><td><b>sum</b></td><td></td></tr>";

但我不知道该怎么办。请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

$sum = 0;
while($b=mysql_fetch_array($a))
{
    //your codes

    $sum += $fields //fields you want to sum up

    echo "<tr><td> 'Sum:'. $sum ... </td></tr>";
}

echo "</table>";