Select 来自同一个 table mysql 的每个条件后的行
Select row after each condition from the same table myql
我有一个 table 这样的:
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
status
timestamp
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
我想要一个输出:
date
A
B
2021-01-01
2021-01-01 06:03:00
2021-01-02 04:00:00
2021-01-02
...
...
2021-01-03
...
...
规则是,1 个记录日是从 5:00:00 -(次日)04:49:49。
'A'是从第一次上线(上线后)开始的时间戳,'B'是从第一次下线(上线后)开始的时间戳。
目前我正在使用此查询来应用记录日规则:
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;
但是我在应用第二条规则来获取时间戳时遇到问题..
更新:
这是测试数据和 table: https://www.db-fiddle.com/f/tDJL2JHwbtSNEurrLEj11r/0
或 http://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);
我有一个 table 这样的:
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 | status | timestamp |
---|---|---|
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 |
我想要一个输出:
date | A | B |
---|---|---|
2021-01-01 | 2021-01-01 06:03:00 | 2021-01-02 04:00:00 |
2021-01-02 | ... | ... |
2021-01-03 | ... | ... |
规则是,1 个记录日是从 5:00:00 -(次日)04:49:49。 'A'是从第一次上线(上线后)开始的时间戳,'B'是从第一次下线(上线后)开始的时间戳。
目前我正在使用此查询来应用记录日规则:
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;
但是我在应用第二条规则来获取时间戳时遇到问题..
更新: 这是测试数据和 table: https://www.db-fiddle.com/f/tDJL2JHwbtSNEurrLEj11r/0 或 http://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);