我的查询从数据库获取以下数据。
这是正确的,但我还需要显示所有行的总和,如下面的总数..
我该怎么做......
我需要在最后一行显示totalbooking列的总和..
bookno bookingdate totalbooking
401 15-03-2015 1
401 16-03-2015 2
404 15-03-2015 1
Total 4
以下是我的代码......
<?php
if($_POST && isset($_POST['Submit']))
{
if($_POST['hawkername']=='All')
{
$cityname = $_SESSION['Auth']['city'];
$data = $database->getRows("SELECT RE.book_no AS book, RE.bookingdate AS bookingdate, HM.hawker_name AS hawkername,SAM.tehsil AS centername,
COUNT( DISTINCT RE.receipt_no ) AS totalbooking,SUM(DISTINCT RE.receipt_no) AS final FROM receipt_entry RE INNER JOIN hawker_master HM ON HM.id = RE.hawkername INNER JOIN sub_agent_master SAM ON SAM.id = RE.subagentname WHERE RE.city_name = :cityname GROUP BY RE.hawkername, RE.book_no",array(':cityname'=>$cityname));
}
?>
<?php if (is_array($data)) { foreach($data as $row){ ?>
<tr>
<td><?php echo htmlspecialchars($row['book']); ?></td>
<td><?php echo htmlspecialchars($row['bookingdate']); ?></td>
<td><?php echo htmlspecialchars($row['totalbooking']); ?></td>
</tr>
<?php } } ?>
<tr>
<td>Total</td>
<td></td>
</tr>
答案 0 :(得分:1)
将您的查询更改为:
SELECT
(SELECT SUM(DISTINCT RE.receipt_no) FROM FROM receipt_entry RE) as totalbooking,
RE.book_no AS book, RE.bookingdate AS bookingdate,
...
答案 1 :(得分:0)
<?php if (is_array($data)) { $total = 0; foreach($data as $row){ $total += $row['totalbooking']; ?>
<tr>
<td><?php echo htmlspecialchars($row['book']); ?></td>
<td><?php echo htmlspecialchars($row['bookingdate']); ?></td>
<td><?php echo htmlspecialchars($row['totalbooking']); ?></td>
</tr>
<?php $i++; } } ?>
<tr>
<td colspan="4"></td>
<td style="font-size:13px; font-weight:bold;">Total</td>
<td style="font-size:13px; font-weight:bold;"><?php echo $total; ?></td>
</tr>