在PHP中,当mysql表中的开始时间大于1小时时,获取Null

在PHP中,当mysql表中的开始时间大于1小时时,获取Null,php,mysql,Php,Mysql,我有一个基于时间计算成本的表格。我需要显示基于时间的表格,限制为1小时。如果时间大于1小时,则成本列中应显示null。我如何才能做到这一点 dashboard.php <?php $host = "localhost"; $user = "root"; $pass = ""; $db_name =

我有一个基于时间计算成本的表格。我需要显示基于时间的表格,限制为1小时。如果时间大于1小时,则成本列中应显示null。我如何才能做到这一点

dashboard.php

        <?php
                    $host    = "localhost";
                    $user    = "root";
                    $pass    = "";
                    $db_name = "test2";
                    $lastId="";


                     $date = strtotime('now') - 3600;  
                     print_r($date);  
                     $week = strtotime('now') - 604800;  
                     $month = strtotime('now') - 2592000 ;        

                    //create connection
                    $con = mysqli_connect($host, $user, $pass, $db_name);

                    //test if connection failed
                    $result = mysqli_query($con,"SELECT SUM(cost) FROM track_data WHERE start_time > 
                      DATE_SUB(NOW(), INTERVAL 1 HOUR) ");

                    $result1 =  mysqli_query($con,"SELECT SUM(cost) FROM track_data WHERE start_time > 
                      DATE_SUB(NOW(), INTERVAL 1 WEEK)");

                    $result2 =  mysqli_query($con,"SELECT SUM(cost) FROM track_data WHERE start_time > 
                      DATE_SUB(NOW(), INTERVAL 1 MONTH)");


                    ?>

    <div class="col-md-6 col-md-offset-3">
            <table class="table table-hover table-striped">    
            <tr class="t_head"> 
              <th>  Days </th>
              <th> Usage  </th>
              <th> Total Cost  </th>

            </tr>       
    <?php


          while($row = mysqli_fetch_array($result))


    {



      echo "<tr id='emp_salary'>"; 

     echo "<td> Today </td>";
      echo "<td>1 Hour</td>";
    echo "<td>" .$row[0]. "</td>";

    echo "</tr>";
    }
?>
</table>
</div>

天
用法
总成本

您应该使用
COALESCE

因为如果sql查询不返回任何行或行的总和为空,那么您的回送实际上不会回送任何内容

试试这个

$result = mysqli_query($con,"SELECT COALESCE(SUM(cost),0) FROM track_data 
WHERE start_time > DATE_SUB(NOW(), INTERVAL 1 HOUR) ");

您应该使用
COALESCE

因为如果sql查询不返回任何行或行的总和为空,那么您的回送实际上不会回送任何内容

试试这个

$result = mysqli_query($con,"SELECT COALESCE(SUM(cost),0) FROM track_data 
WHERE start_time > DATE_SUB(NOW(), INTERVAL 1 HOUR) ");

你试图解决这个问题的方法是什么?很可能,您的SQL查询在该列中没有返回任何内容?@NicoHaase我需要从表中选择的成本总和。对于屏幕截图中显示的特定行,您的查询究竟返回什么?@NicoHaase它检查时间间隔,如果时间间隔大于1小时,它将返回空列,而不是应该返回的空列null或zero您试图解决什么问题?很可能,您的SQL查询在该列中没有返回任何内容?@NicoHaase我需要从表中选择的成本总和。对于屏幕截图中显示的特定行,您的查询究竟返回什么?@NicoHaase它检查时间间隔,如果时间间隔大于1小时,它将返回空列,而不是应该返回的空列null或zero乐意帮助您:)乐意帮助您:)