简体   繁体   English

MySQL用于显示表中每小时的条目数

[英]MySQL to display number of entries per hour in a table

New link - results off by 1 I need to echo in a table the number of entries per hour for a given date, EVEN IF they are 0. This seems to be happening but in a weird way... As you can see, the hours column is not playing ball as this should be 0:00-1 .... 22.00-23.00 etc. The total number of result expected is 19 which is also correct when you sum the results together. 新链接 - 结果关闭1我需要在表格中回显给定日期每小时的条目数,即使它们为0.这似乎正在发生但是以一种奇怪的方式...正如您所看到的,小时栏不打球,因为这应该是0:00-1 .... 22.00-23.00等。预期的结果总数是19,当你将结果加在一起时也是正确的。 Got to the point where I now don't know what is happening where in the script and no idea where to start fixing the formatting issues. 到了我现在不知道脚本中发生了什么的地步,不知道从哪里开始修复格式问题。 Any help much appreciated. 任何帮助非常感谢。 Current Result in HTML Page HTML页面中的当前结果

// Get list of times per hour for reader 
$sqltimea = "SELECT   CONCAT(Hour, ':00-', Hour+1, ':00') AS hours,
 COUNT(r.readerid) AS tapcount
FROM 
   (
     SELECT  '0' AS Hour
     UNION ALL SELECT  '1' UNION ALL SELECT  '2' UNION ALL SELECT  '3'
     UNION ALL SELECT  '4' UNION ALL SELECT  '5' UNION ALL SELECT  '6'
     UNION ALL SELECT  '7' UNION ALL SELECT  '8' UNION ALL SELECT  '9'
     UNION ALL SELECT '10' UNION ALL SELECT '11' UNION ALL SELECT '12'
     UNION ALL SELECT '13' UNION ALL SELECT '14' UNION ALL SELECT '15'
     UNION ALL SELECT '16' UNION ALL SELECT '17' UNION ALL SELECT '18'
     UNION ALL SELECT '19' UNION ALL SELECT '20' UNION ALL SELECT '21'
     UNION ALL SELECT '22' UNION ALL SELECT '23'
 ) AS h
  LEFT JOIN taps t ON HOUR(t.`time`) = Hour 
AND
    DATE(t.time) = '2016-01-15' LEFT JOIN
 readers r
 ON r.readerid = t.readerid AND r.type = 'A'
GROUP BY Hour
ORDER BY Hour";
$qtimea = mysql_query($sqltimea);

// THE CHALLENGE! Put it in a table!

$hours = array("0:00-1:00", "1:00-2:00", "2:00-3:00", "3:00-4:00", "4:00-5:00", "5:00-6:00", "6:00-7:00", "7:00-8:00", "8:00-9:00", "9:00-10:00", "11:00-12:00", "12:00-13:00", "13:00-14:00", "14:00-15:00", "15:00-16:00", "16:00-17:00", "17:00-18:00", "18:00-19:00", "19:00-20:00", "20:00-21:00", "21:00-22:00", "22:00-23:00", "23:00-24:00");

for($i = 0; $i <= 23; $i++) {
    while($timea = mysql_fetch_assoc($qtimea)) {
        $tapsa = $timea['tapcount'];
        echo "<tr><td>" . $hours[$i] . "</td><td>" . $tapsa . "</td></tr>";
    }
}
?>

Im not expert on PHP, but your i variable doesnt get incremented inside the while just add $hours[$i++] or just get your label from $timea['hours'] 我不是专家PHP,但你i变犯规得到增加的内部while只需加$hours[$i++]或只是从你的标签$timea['hours']

And you probably dont need the for just initialize i = 0 你可能不需要在for刚刚初始化i = 0

while($timea = mysql_fetch_assoc($qtimea)) {
        $tapsa = $timea['tapcount'];
        $hour_label = $timea['hours'];
        echo "<tr><td>" . $hour_label . "</td><td>" . $tapsa . "</td></tr>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM