使用 mysql db 中的数据在 html 表中显示给定时间的活动并发进程数


Use data from mysql db to present the number of active concurrent processes at a given time in a html table

我的数据库中有一个数据集,如下所示,显示进程的开始时间和持续时间(以秒为单位):

+---------------------+----------+
| start_time          | duration |
+---------------------+----------+
| 2013-10-01 10:00:00 |     15.6 |
| 2013-10-01 10:00:03 |      0.8 |
| 2013-10-01 10:00:03 |      4.5 |
| 2013-10-01 10:00:14 |     22.8 |
| 2013-10-01 10:00:28 |     30.9 |
+---------------------+----------+

有了这些数据,我希望能够以每 15 秒的间隔在浏览器中显示一个表格,以查看在指定时间间隔内有多少进程处于活动状态。用户将能够指定日期和时间范围,以便可以像这样使用查询:

SELECT start_time, duration FROM process_period
WHERE start_time >= "2013-10-01 10:00:00" AND start_time < "2013-10-24 11:00:00"
最好的

方法是什么?所需的输出应如下所示:

Interval time:               Number of active processes:
2013-10-01 10:00:00                      1
2013-10-01 10:00:15                      2
2013-10-01 10:00:30                      2
2013-10-01 10:00:45                      1

您必须在查询中使用两件事。要计算在特定时间启动的进程数,请使用"GROUPBY",并使用"HAVING"设置间隔。由于您使用的是"GROUPBY",因此查询中不可能使用"WHERE"子句。检查后续小提琴http://sqlfiddle.com/#!2/a82fa/4/0

编辑:或者如果您希望在特定间隔内进行进程计数,请执行以下操作

select count(*) as process_count from processdata where start_time between '2013-10-01 10:15:00' and '2013-10-01 10:23:00'