$ row的总和基于特定时间字段而不仅仅是总计

时间:2014-10-26 06:56:54

标签: php mysql sum running-total subtotal

此代码可以很好地显示给定日期的预订列表,并将客人和餐桌计数总计为一整天。汇总所有退回的预订。

我想要做或想出的是一种基于特定时间间隔获得总计的方法。 例如,8:00,9:00,10:00等多少客人和桌子......

我可以看到计算总和的位置,但需要帮助添加一种方法来添加变量以查看reservation_time并按小时而不是仅按日总计。

        $tablesum ++;
        $guestsum += $row->reservation_pax;
<?php echo $guestsum;?>&nbsp;&nbsp;<?php echo _guest_summary;?>

完整的代码,用于提取数据,然后将其总结:

<?php
    // Clear reservation variable
    $reservations ='';

    if ($_SESSION['page'] == 1) {
        $reservations = querySQL('all_reservations');
    }else{
        $reservations = querySQL('reservations');
    }

    // reset total counters
    $tablesum = 0;
    $guestsum = 0;

    if ($reservations) {

        //start printing out reservation grid
        foreach($reservations as $row) {
            // reservation ID
            $id = $row->reservation_id;
            $_SESSION['reservation_guest_name'] = $row->reservation_guest_name;
            // check if reservation is tautologous
            $tautologous = querySQL('tautologous');

        echo "<tr id='res-".$id."'>";
        echo "<td";
        // daylight coloring
        if ($row->reservation_time > $daylight_evening){
            echo " class='evening noprint'";
        }else if ($row->reservation_time > $daylight_noon){
            echo " class='afternoon noprint'";
        }else if ($row->reservation_time < $daylight_noon){
            echo " class='morning noprint'";
        }

        echo " style='width:10px !important; padding:0px;'>&nbsp;</td>";
        echo "<td id='tb_time'";
        // reservation after maitre message
        if ($row->reservation_timestamp > $maitre['maitre_timestamp'] && $maitre['maitre_comment_day']!='') {
            echo " class='tautologous' title='"._sentence_13."' ";
        }
        echo ">";
        echo "<strong>".formatTime($row->reservation_time,$general['timeformat'])."</strong></td>";
        echo "<td id='tb_pax'><strong class='big'>".$row->reservation_pax."</strong>&nbsp;&nbsp;<span class='noprint'>";
        $tablesum ++;
        $guestsum += $row->reservation_pax;
        }
    }
    ?>

    <?php echo $guestsum;?>&nbsp;&nbsp;<?php echo _guest_summary;?>

好的我越来越近了:能够得到它并显示......虽然我需要缩小一个参数。这给了我所有预订的总客人数(reservation_pax)。我需要在给定的日期或会话期间执行此操作。该页面仅显示所选日期的预订,但脚本会在8:00预订所有天数。

只需要帮助缩小会话页面已经为正在查看的特定日期创建。

$result = mysql_query("SELECT SUM(reservation_pax) FROM reservations WHERE reservation_time = '8:00'"); 
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}

while($row = mysql_fetch_array($result))
{

   echo $row['SUM(reservation_pax)'];
}

2 个答案:

答案 0 :(得分:0)

你需要生成具有时间间隔的系列,假设你有一些超过15行的表(我称之为lookup_table)你可以用uservars来做这个。

SELECT d.starttime, d.endtime, COUNT(r.id) AS total
FROM
    (
     SELECT
        @starttime := @starttime + INTERVAL 1 HOUR starttime,
        @endtime := @endtime + INTERVAL 1 HOUR endtime
     FROM
        lookup_table
     CROSS JOIN (SELECT @starttime := CURRENT_DATE + INTERVAL 7 HOUR,
                        @endtime := CURRENT_DATE + INTERVAL 8 HOUR - INTERVAL 1 SECOND) v
     LIMIT 15
    ) d
LEFT JOIN
    reservations r
    ON r.reservation_time BETWEEN d.starttime AND d.endtime
GROUP BY d.starttime, d.endtime

如果您需要从预订中过滤掉一些数据,则需要将其添加到连接条件中。

这是基于您提供的信息(不多)。

答案 1 :(得分:0)

$result = mysql_query("SELECT SUM(reservation_pax) FROM reservations WHERE reservation_time = '8:00:00' AND reservation_date = '2014-10-27' AND reservation_hidden ='0'") ; 
if($result === FALSE) {
    die(mysql_error()); // TODO: better error handling
}

while($row = mysql_fetch_array($result))
{
   echo $row['SUM(reservation_pax)'];
}

?>