我正在尝试使用MySQL和PHP生成每小时报告。
我想在08:00:00 - 09:00:00之间显示有多少案例,就像24小时这样,并将它划分为明确的日期。现在我得到的结果,但我创建了多个结果查询,如下所示:
$result = mysqli_query($con,"SELECT COUNT(IST_In_Time), DATE(IST_In_Time) FROM spillreport WHERE TIME (IST_In_Time) BETWEEN '19:00:00' AND '20:00:00' GROUP BY DATE(IST_In_Time)");
$result1 = mysqli_query($con,"SELECT COUNT(IST_In_Time), DATE(IST_In_Time) FROM spillreport WHERE TIME (IST_In_Time) BETWEEN '08:00:00' AND '09:00:00' GROUP BY DATE(IST_In_Time)");
并在循环中存储不同的内容:
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['DATE(IST_In_Time)'] . "</td>";
echo "<td>" . $row['COUNT(IST_In_Time)'] . "</td>";
echo "<td></td>";
}
while($row = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td>" . $row['DATE(IST_In_Time)'] . "</td>";
echo "<td></td>";
echo "<td>" . $row['COUNT(IST_In_Time)'] . "</td>";
}
我知道这种不好的做法,但我没有时间采取更好的方法。我希望在一个表中显示行将根据日期自动增加,列将预定义为上午7:00至8:00,上午8:00至晚上9:00等等。
修改 使用以下答案我得到以下结果:
查询:SELECT DATE(IST_In_Time) date,HOUR(IST_In_Time) hour,COUNT(*) count FROM spillreport WHERE DATE(IST_In_Time)>= '2014-07-22 00:00:00' AND DATE(IST_In_Time)<= '2014-07-26 00:00:00' GROUP BY HOUR(IST_In_Time), DATE(IST_In_Time) ORDER BY DATE(IST_In_Time)ASC
date hour count
2014-07-22 19 1
2014-07-22 14 1
2014-07-23 18 28
2014-07-23 15 1
2014-07-23 19 26
2014-07-23 17 1
2014-07-23 20 8
2014-07-24 11 34
2014-07-24 19 2
2014-07-24 8 1
2014-07-24 12 35
2014-07-24 13 23
2014-07-24 15 37
2014-07-24 14 52
2014-07-24 10 34
2014-07-24 16 59
2014-07-24 9 15
2014-07-24 17 46
2014-07-24 18 25
2014-07-25 8 1
2014-07-26 19 1
2014-07-26 8 2
但是我想只分组唯一的日期,小时应该像这样分开:
Hours 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
2014-07-22 - - - - - - - - - - - - - - 1 - - - - 1 - - - -
2014-07-23 - - - - - - - - - - - - - - - 1 - 1 28 26 8 - - -
2014-07-24
2014-07-25
2014-07-26
喜欢这个!
编辑2:
select client_id,
max(case when rownum = 1 then date end) Date1,
max(case when rownum = 2 then date end) Date2,
max(case when rownum = 3 then date end) Date3
from
(
select client_id,
date,
@row:=if(@prev=client_id, @row,0) + 1 as rownum,
@prev:=client_id
from yourtable, (SELECT @row:=0, @prev:=null) r
order by client_id, date
) s
group by client_id
order by client_id, date
我有这个代码来创建数据透视表,任何人都可以指导我如何为我创建这个?
答案 0 :(得分:1)
我认为你想按小时计算,所以评论你可以使用HOUR。例如今天我必须从一张桌子上计算每小时每小时的事件数量:
mysql> SELECT DATE(event_time) date,HOUR(event_time) hour,COUNT(*) count
FROM events WHERE event_time > curdate() GROUP BY DATE(event_time),HOUR(event_time);
+------------------+------------------+----------+
| date | hour | count |
+------------------+------------------+----------+
| 2014-07-26 | 0 | 30 |
| 2014-07-26 | 1 | 21 |
| 2014-07-26 | 2 | 11 |
| 2014-07-26 | 3 | 29 |
| 2014-07-26 | 4 | 25 |
| 2014-07-26 | 5 | 22 |
| 2014-07-26 | 6 | 46 |
| 2014-07-26 | 7 | 42 |
| 2014-07-26 | 8 | 26 |
| 2014-07-26 | 9 | 58 |
| 2014-07-26 | 10 | 11 |
+------------------+------------------+----------+
11 rows in set (0.00 sec)
编辑:已更新,可按日期添加缺少的分组。
我建议按日期,小时和按日期,小时排序进行分组,以获得精确排序的结果集。这应该产生PHP中任何后期处理所需的数据。之后,它是一个PHP格式化工作。
答案 1 :(得分:0)