数周以来,我一直在绞尽脑汁思考如何正确地实现这一目标,一直在使用 PHP 和 mySQL,但我编写的每个公式或函数似乎都缺少一些东西。这正是我想要做的......
-填满登录记录的mySQL数据库。
-字段是 RECORD、START_TIME、DURATION、CUSTOMERID
-RECORD是唯一键
-START_TIME 格式为“2013-12-12 10:32:59”
-DURATION 以秒为单位
-CUSTOMERID为用户唯一账号
允许每个用户同时登录 3 次,任何超过 3 次的登录我都需要计算“超龄”期间该用户所有 session 的总 DURATION 秒数。
所以一个例子是..
RECORD | START_TIME | DURATION | CUSTOMERID
1 2013-1-1 12:00:00 10 BILLYBOB
2 2013-1-1 12:01:00 600 BILLYBOB
3 2013-1-1 12:04:00 1200 BILLYBOB
4 2013-1-1 12:05:20 500 BILLYBOB
5 2013-1-1 12:06:30 600 BILLYBOB
6 2013-1-1 16:00:00 100 BILLYBOB
7 2013-1-1 18:00:00 300 BILLYBOB
在这种情况下,它会返回记录 2、3、4、5,因为有超过 3 个同时 session ,然后返回这些记录的总持续时间,即 2900。
这对 MySQL 要求过高吗?
最佳答案
好的,我明白了......
首先,您必须找到每个用户的时间片。假设他/她从 10:00:00 到 10:09:59 和从 10:10:00 到 10:19:59 以及从 10:00:00 到 10:29:59 工作。这为您提供了三个时间片:10:00:00 到 10:09:59( session 1 和 3)、10:10:00 到 10:19:59( session 2 和 3)和 10:20:00 到 10 :29:59(第 3 节)。
然后您计算每个时间片中活跃的 session ,并删除只有三个或更少 session 活跃的片。找到那些违反的时间片后,您可以选择当时处于事件状态的 session 。
完整声明如下:
select *
from sessions
where exists
(
select *
from
(
select start_times.customerid, start_times.start_time, min(end_times.end_time) as end_time
from
(
select customerid, start_time from sessions
union
select customerid, date_add(start_time, interval duration second) from sessions
) start_times
join
(
select customerid, date_add(start_time, interval duration - 1 second) as end_time from sessions
union
select customerid, date_add(start_time, interval -1 second) from sessions
) end_times
on (start_times.customerid = end_times.customerid and start_times.start_time < end_times.end_time)
group by start_times.customerid, start_times.start_time
) time_slices
where
(
select count(*)
from sessions
where time_slices.customerid = sessions.customerid
and
(
time_slices.start_time between sessions.start_time and date_add(sessions.start_time, interval duration - 1 second)
and
time_slices.end_time between sessions.start_time and date_add(sessions.start_time, interval duration - 1 second)
)
) > 3
and time_slices.customerid = sessions.customerid
and
(
time_slices.start_time between sessions.start_time and date_add(sessions.start_time, interval duration - 1 second)
and
time_slices.end_time between sessions.start_time and date_add(sessions.start_time, interval duration - 1 second)
)
)
;
这是 SQL fiddle :http://sqlfiddle.com/#!2/12c47/1 .
关于php - MySQL 搜索超过允许限制的并发时间戳,然后计算 SUM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20547830/