duanrao3371 2014-07-26 09:16
浏览 28
已采纳

如何在一个表中只存储多个结果MySql和PHP

I am trying to generate Hourly Report using MySQL and PHP.

I want to show how many cases came in between 08:00:00 - 09:00:00 like this of 24 hours and Divide it Date Wise. Right now I am getting the result but I have created multiple result queries like this:

$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)"); 

And storing it different while loops:

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>";
}

I know this bad practice, but I don't have time to take a better approach. I want to display in a single table where rows will automatically increase according to dates and columns will be predefined like 7:00am-8:00am , 8:00am-9:00am and so on..

EDIT: Using Below Answers I got the following Result:

Query: 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

Result:

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

But I want to group only unique dates and hours should be divided horizontally like this:

    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

Like this!

EDIT 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

I got this code to create pivot table, can anybody guide me how do I create this for mine?

  • 写回答

2条回答 默认 最新

  • dongshedan4672 2014-07-26 09:29
    关注

    I think you want counts by the hour, so as commented you can use HOUR. e.g. to count number of events per hour today from a table I have to hand:

     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)
    

    Edit: Updated to add missing grouping by date.

    I'd suggest grouping by date,hour and ordering by date,hour too to get a nicely ordered result set. That should produce the data you need for any post processing in PHP. After that it's a PHP formatting job.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置