1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| --------------
START TRANSACTION
--------------
--------------
set session collation_connection = "latin1_general_ci"
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE IF NOT EXISTS `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test`
( `id` integer unsigned NOT NULL primary key,
`userid` smallint unsigned NOT NULL,
`date` datetime NOT NULL,
`action` char(06) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`id`,`userId`,`date`,`action`) VALUES
( 1,45,from_unixtime(1681223015),'edit'),( 2,55,from_unixtime(1681225805),'edit'),( 3,55,from_unixtime(1681223685),'edit'),
( 4,55,from_unixtime(1681220885),'add'), ( 5,55,from_unixtime(1681215885),'edit'),( 6,55,from_unixtime(1681223185),'edit'),
( 7,55,from_unixtime(1681221885),'edit'),( 8,45,from_unixtime(1681225885),'add'), ( 9,55,from_unixtime(1681220885),'delete'),
(10,55,from_unixtime(1681225085),'add'), (11,55,from_unixtime(1681223885),'add'), (12,55,from_unixtime(1681205885),'edit'),
(13,55,from_unixtime(1681220189),'edit'),(14,55,from_unixtime(1681221885),'edit'),(15,55,from_unixtime(1681225885),'add')
--------------
--------------
select * from `test`
--------------
+----+--------+---------------------+--------+
| id | userid | date | action |
+----+--------+---------------------+--------+
| 1 | 45 | 2023-04-11 16:23:35 | edit |
| 2 | 55 | 2023-04-11 17:10:05 | edit |
| 3 | 55 | 2023-04-11 16:34:45 | edit |
| 4 | 55 | 2023-04-11 15:48:05 | add |
| 5 | 55 | 2023-04-11 14:24:45 | edit |
| 6 | 55 | 2023-04-11 16:26:25 | edit |
| 7 | 55 | 2023-04-11 16:04:45 | edit |
| 8 | 45 | 2023-04-11 17:11:25 | add |
| 9 | 55 | 2023-04-11 15:48:05 | delete |
| 10 | 55 | 2023-04-11 16:58:05 | add |
| 11 | 55 | 2023-04-11 16:38:05 | add |
| 12 | 55 | 2023-04-11 11:38:05 | edit |
| 13 | 55 | 2023-04-11 15:36:29 | edit |
| 14 | 55 | 2023-04-11 16:04:45 | edit |
| 15 | 55 | 2023-04-11 17:11:25 | add |
+----+--------+---------------------+--------+
--------------
select `userid`,
`date` as arret,
`val` as relance,
`duree`
from ( select `id`,
`userid`,
`date`,
@val:=if(`userid`=@prev, @val, NULL) as val,
timestampdiff(minute,`date`,@val) as duree,
@val:=`date`,
@prev:=`userid`
from `test`
cross join ( SELECT @val:=NULL,@prev=NULL) as x
order by `userid` desc, `date` desc
) as x
where `duree` >= 20
order by `userid`, `date`
--------------
+--------+---------------------+---------------------+-------+
| userid | arret | relance | duree |
+--------+---------------------+---------------------+-------+
| 45 | 2023-04-11 16:23:35 | 2023-04-11 17:11:25 | 47 |
| 55 | 2023-04-11 11:38:05 | 2023-04-11 14:24:45 | 166 |
| 55 | 2023-04-11 14:24:45 | 2023-04-11 15:36:29 | 71 |
| 55 | 2023-04-11 16:04:45 | 2023-04-11 16:26:25 | 21 |
| 55 | 2023-04-11 16:38:05 | 2023-04-11 16:58:05 | 20 |
+--------+---------------------+---------------------+-------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager