简体   繁体   English

Select 行后的每个条件来自同一个表 myql

[英]Select row after each condition from the same table myql

I have a table like this:我有一张这样的桌子:

CREATE TABLE IF NOT EXISTS `logging` (
  `id` int(6) unsigned NOT NULL,
  `status` varchar(150) NOT NULL,
  `timestamp` DATETIME NOT NULL,
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `logging` (`id`, `status`, `timestamp`) VALUES
  ('1', 'logout', '2021-01-01 05:01:00'),
  ('2', 'login', '2021-01-01 06:02:00'),
  ('3', 'online', '2021-01-01 06:03:00'),
  ('4', 'away', '2021-01-01 06:04:00'),
  ('5', 'online', '2021-01-01 06:05:00'),
  ('6', 'logout', '2021-01-02 04:00:00'),
  ('7', 'login', '2021-01-02 04:05:00'),
  ('8', 'online', '2021-01-02 04:07:00'),
  ('9', 'logout', '2021-01-02 04:55:00');
id ID status地位 timestamp时间戳
1 1 logout登出 2021-01-01 05:01:00 2021-01-01 05:01:00
2 2 login登录 2021-01-01 06:02:00 2021-01-01 06:02:00
3 3 online在线的 2021-01-01 06:03:00 2021-01-01 06:03:00
4 4 away离开 2021-01-01 06:04:00 2021-01-01 06:04:00
5 5 online在线的 2021-01-01 06:05:00 2021-01-01 06:05:00
6 6 logout登出 2021-01-02 04:00:00 2021-01-02 04:00:00
7 7 login登录 2021-01-02 04:05:00 2021-01-02 04:05:00
8 8 online在线的 2021-01-02 04:07:00 2021-01-02 04:07:00
9 9 logout登出 2021-01-02 04:55:00 2021-01-02 04:55:00

i want to have an output:我想要一个 output:

date日期 A一个 B
2021-01-01 2021-01-01 2021-01-01 06:03:00 2021-01-01 06:03:00 2021-01-02 04:00:00 2021-01-02 04:00:00
2021-01-02 2021-01-02 ... ... ... ...
2021-01-03 2021-01-03 ... ... ... ...

the rule is, 1 log day is from 5:00:00 - (next day) 04:49:49.规则是,1 个日志日是从 5:00:00 到(第二天)04:49:49。 'A' is timestamp start from the first online (after login), 'B' is timestamp from the first logout (after online). 'A' 是从第一次在线(登录后)开始的时间戳,'B' 是从第一次注销(在线后)开始的时间戳。

currently i'm using this query to apply the log day rule:目前我正在使用此查询来应用日志日规则:

select dt, timestamp from ( 
    select id, left(timestamp, 10) dt, timestamp, status
    from logging
    where TIME(timestamp)  >= '05:00:00' and status = 'online'
    union all 
    select id, left((timestamp-interval 1 day), 10) dt ,timestamp, status
    from logging
    where TIME(timestamp)  < '05:00:00' and status = 'online'
    ) x group by dt;

But i have problem to apply the second rule to get the timestamp..但我有问题应用第二条规则来获取时间戳..

Updated: Here's the test data and table: https://www.db-fiddle.com/f/tDJL2JHwbtSNEurrLEj11r/0 or http://sqlfiddle.com/#!9/aadb80/2更新:这是测试数据和表格: https://www.db-fiddle.com/f/tDJL2JHwbtSNEurrLEj11r/0http://sqlfiddle.com/#!9/aadb80/2

SELECT t1.ts A, t3.ts B
FROM test t1
JOIN test t2 ON t1.ts < t2.ts
JOIN test t3 ON t2.ts < t3.ts
WHERE t1.status = 'login'
  AND t2.status = 'online'
  AND t3.status = 'logout'
  AND NOT EXISTS ( SELECT NULL
                   FROM test t4
                   WHERE t1.ts < t4.ts
                     AND t4.ts < t2.ts
                     AND t4.status IN ('login', 'online', 'logout') )
  AND NOT EXISTS ( SELECT NULL
                   FROM test t5
                   WHERE t2.ts < t5.ts
                     AND t5.ts < t3.ts
                     AND t5.status IN ('login', 'logout') )
  AND DATE(t1.ts - INTERVAL 5 HOUR) = DATE(t3.ts - INTERVAL '04:49:49' HOUR_SECOND);

fiddle 小提琴

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

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