【发布时间】:2019-01-25 12:24:15
【问题描述】:
我有一个表,其中有两列。一列(callstart)显示呼叫开始时间。另一列(callend)显示呼叫结束时间。
现在我想为单日制作结果,该结果将处于当时正在运行多少个呼叫的情况。 那时已经启动了多少次通话。
表:
+-------------+-------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------------------+-------+
| id | int(11) | NO | | NULL | |
| user_id | int(11) | YES | | 0 | |
| callstart | datetime | NO | | 0000-00-00 00:00:00 | |
| callend | datetime | NO | | 0000-00-00 00:00:00 | |
+-------------+-------------+------+-----+---------------------+-------+
4 rows in set (0.00 sec)
预期结果:
+----+-----------+---------------------+----+-----+
| id | user_id | date | cc | cps |
+----+-----------+---------------------+----+-----+
| 1 | 3 | 2018-04-02 19:47:27 | 0 | 1 |
| 2 | 3 | 2018-04-02 19:48:52 | 1 | 1 |
| 3 | 3 | 2018-04-02 19:48:53 | 1 | 0 |
| 4 | 3 | 2018-04-02 19:48:54 | 1 | 0 |
| 5 | 3 | 2018-04-02 19:48:55 | 1 | 0 |
+----+-----------+---------------------+----+-----+
在上面的结果中:
cc :在特定时间运行的并发调用。 cps : 在特定时间开始的调用。
注意:
1. mysql version is 5.5.
2. In above table,Data will be available only for single day( A call which has been ended on day upto call has been started on a day).
3.I have make it working with php script.But its take long time to manage 6 months record.So i would like to make query which can gives result for every single day.
小提琴:http://sqlfiddle.com/#!9/4c042/5
预计结果准时:2018-08-20 12:20:34 需要显示两行
accountid | callstart | cc |cps|
--------------------------------------------
56 | 2018-08-20 12:20:24 | 1 | 1 |
56 | 2018-08-20 12:20:25 | 1 | 0 |
56 | 2018-08-20 12:20:26 | 1 | 0 |
56 | 2018-08-20 12:20:27 | 1 | 0 |
56 | 2018-08-20 12:20:28 | 1 | 0 |
56 | 2018-08-20 12:20:29 | 1 | 0 |
56 | 2018-08-20 12:20:30 | 1 | 0 |
56 | 2018-08-20 12:20:31 | 1 | 0 |
56 | 2018-08-20 12:20:32 | 1 | 0 |
56 | 2018-08-20 12:20:33 | 1 | 0 |
56 | 2018-08-20 12:20:34 | 3 | 2 |
56 | 2018-08-20 12:20:35 | 3 | 0 |
56 | 2018-08-20 12:20:36 | 3 | 0 |
56 | 2018-08-20 12:20:37 | 3 | 0 |
56 | 2018-08-20 12:20:38 | 3 | 0 |
56 | 2018-08-20 12:20:39 | 3 | 0 |
56 | 2018-08-20 12:20:40 | 3 | 0 |
56 | 2018-08-20 12:20:41 | 2 | 0 |
56 | 2018-08-20 12:20:42 | 2 | 0 |
56 | 2018-08-20 12:20:43 | 2 | 0 |
56 | 2018-08-20 12:20:44 | 2 | 0 |
56 | 2018-08-20 12:20:45 | 2 | 0 |
56 | 2018-08-20 12:20:46 | 2 | 0 |
56 | 2018-08-20 12:20:47 | 2 | 0 |
56 | 2018-08-20 12:20:48 | 2 | 0 |
56 | 2018-08-20 12:20:49 | 2 | 0 |
56 | 2018-08-20 12:20:50 | 0 | 0 |
58 | 2018-08-20 12:20:29 | 1 | 1 |
58 | 2018-08-20 12:20:30 | 1 | 0 |
58 | 2018-08-20 12:20:31 | 1 | 0 |
58 | 2018-08-20 12:20:32 | 1 | 0 |
58 | 2018-08-20 12:20:33 | 1 | 0 |
58 | 2018-08-20 12:20:34 | 3 | 2 |
58 | 2018-08-20 12:20:35 | 3 | 0 |
58 | 2018-08-20 12:20:36 | 3 | 0 |
58 | 2018-08-20 12:20:37 | 3 | 0 |
58 | 2018-08-20 12:20:38 | 3 | 0 |
58 | 2018-08-20 12:20:39 | 3 | 0 |
58 | 2018-08-20 12:20:40 | 3 | 0 |
58 | 2018-08-20 12:20:41 | 3 | 0 |
58 | 2018-08-20 12:20:42 | 3 | 0 |
58 | 2018-08-20 12:20:43 | 3 | 0 |
58 | 2018-08-20 12:20:44 | 3 | 0 |
58 | 2018-08-20 12:20:45 | 3 | 0 |
58 | 2018-08-20 12:20:46 | 3 | 0 |
58 | 2018-08-20 12:20:47 | 3 | 0 |
58 | 2018-08-20 12:20:48 | 3 | 0 |
58 | 2018-08-20 12:20:49 | 2 | 0 |
58 | 2018-08-20 12:20:50 | 2 | 0 |
58 | 2018-08-20 12:20:51 | 2 | 0 |
58 | 2018-08-20 12:20:52 | 2 | 0 |
58 | 2018-08-20 12:20:53 | 2 | 0 |
58 | 2018-08-20 12:20:54 | 2 | 0 |
58 | 2018-08-20 12:20:55 | 2 | 0 |
58 | 2018-08-20 12:20:56 | 2 | 0 |
--------------------------------------------
任何人请分享一些建议。
【问题讨论】:
-
您能分享您当前的查询吗?
-
是的,现在正在使用查询:select count(id) as count,user_id from table where callstart ='".$datetime. "' 和 accountid > 0 并按 accountid 分组。对于我从 php 脚本中使用的日期时间。