简体   繁体   English

确定 Mysql 中的用户会话

[英]Determine user sessions in Mysql

how can I determine the session times of these fictitious users?如何确定这些虚构用户的会话时间? As an example, for user 1 I assume that between 08:24:08 and 15:08:20 he wasn't there, so I don't add that time interval, I add the other times.例如,对于用户 1,我假设他不在 08:24:08 和 15:08:20 之间,所以我不添加该时间间隔,而是添加其他时间。

So I look for a method to add up (in seconds or minutes) the intermediate times based on this table.因此,我寻找一种方法来根据此表将中间时间相加(以秒或分钟为单位)。 Any ideas.有任何想法吗。

drop table sessions;
CREATE TABLE IF NOT EXISTS sessions (    
    id_user int(11),
    start_date datetime
);

insert into sessions values (1,'2020-02-21 08:24:07');
insert into sessions values (1,'2020-02-21 08:24:08');--> 1 seg (08:24:08 - 08:24:07)
insert into sessions values (1,'2020-02-21 15:08:20');
insert into sessions values (1,'2020-02-21 15:08:21');--> 1 seg (15:08:21 - 15:08:20)
insert into sessions values (1,'2020-02-21 15:08:22');--> 1 seg 
insert into sessions values (2,'2020-02-20 15:08:22');
insert into sessions values (2,'2020-02-20 15:15:22');--> 7 min (15:15:22 - 15:08:22)
insert into sessions values (3,'2020-02-20 15:08:22');
insert into sessions values (3,'2020-02-20 15:15:22');--> 7 min
insert into sessions values (3,'2020-02-20 15:20:22');--> 5 min

This shows you how you can accomplish that in mysql 5.x and higher这向您展示了如何在 mysql 5.x 及更高版本中实现这一目标

Please read the Text at the end for more information请阅读最后的文本以获取更多信息

CREATE TABLE IF NOT EXISTS `sessions` ( id_user int, `start_date` datetime ); insert into `sessions` values (1,'2020-02-21 08:24:07'); insert into `sessions` values (1,'2020-02-21 08:24:08'); insert into `sessions` values (1,'2020-02-21 15:08:20'); insert into `sessions` values (1,'2020-02-21 15:08:21'); insert into `sessions` values (1,'2020-02-21 15:08:22'); insert into `sessions` values (2,'2020-02-20 15:08:22'); insert into `sessions` values (2,'2020-02-20 15:15:22'); insert into `sessions` values (3,'2020-02-20 15:08:22'); insert into `sessions` values (3,'2020-02-20 15:15:22'); insert into `sessions` values (3,'2020-02-20 15:20:22');
\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n
SELECT IF(@iduser = id_user,TIME_FORMAT(TIMEDIFF(`start_date`,@date), '%H:%i:%S'), NULL) Datediff ,@iduser := id_user iduser , @date := `start_date` `start_date` FROM (SELECT * FROM sessions ORDER by id_user ASC, start_date ASC) t ,(SELECT @iduser := 0) a,(SELECT @date := 0) b;
\nDatediff |日期差异 | iduser |用户 | start_date开始日期         \n:------- | :------- | -----: | -----: | :------------------ :------------------\nnull || 1 | 1 | 2020-02-21 08:24:07 2020-02-21 08:24:07\n00:00:01 | 00:00:01 | 1 | 1 | 2020-02-21 08:24:08 2020-02-21 08:24:08\n06:44:12 | 06:44:12 | 1 | 1 | 2020-02-21 15:08:20 2020-02-21 15:08:20\n00:00:01 | 00:00:01 | 1 | 1 | 2020-02-21 15:08:21 2020-02-21 15:08:21\n00:00:01 | 00:00:01 | 1 | 1 | 2020-02-21 15:08:22 2020-02-21 15:08:22\nnull || 2 | 2 | 2020-02-20 15:08:22 2020-02-20 15:08:22\n00:07:00 | 00:07:00 | 2 | 2 | 2020-02-20 15:15:22 2020-02-20 15:15:22\nnull || 3 | 3 | 2020-02-20 15:08:22 2020-02-20 15:08:22\n00:07:00 | 00:07:00 | 3 | 3 | 2020-02-20 15:15:22 2020-02-20 15:15:22\n00:05:00 | 00:05:00 | 3 | 3 | 2020-02-20 15:20:22 2020-02-20 15:20:22\n

db<>fiddle here db<> 在这里小提琴

It uses TIMEDIFF to get the Difference between to rows.它使用TIMEDIFF来获取行之间的差异。

And the uses TIME_FORMAT to get rid of the milliseconds.并且使用TIME_FORMAT来摆脱毫秒。

The new Datediff column is at the start and must stay there so that the Algorithm can work .新的Datediff列位于开头并且必须保留在那里,以便算法可以工作 If you want to reorder it in mysql you need a new outer SELECT query.如果要在 mysql 中对其重新排序,则需要一个新的外部 SELECT 查询。

The final problem with this algorithm is, that it simple makes time1 - time2, it doesn't care if a new day start, it would still make this for every id_user.这个算法的最后一个问题是,它很简单地使 time1 - time2,它不关心新的一天是否开始,它仍然会为每个 id_user 做这个。 It is possible to use a much more sophisticated ORDER BY clauses.可以使用更复杂的 ORDER BY 子句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM