让我们说100是一个开始,101是一个停止事件.这些应该交替,我想在他们不知道的时候找出它们.每次开始都应该有一个先前的停止事件.鉴于此表:
ID EVENT_ID
10 100 // a start event
11 101 // a stop event
12 99 // some irrelevant event
13 100 // .. and so on:
14 101
15 100
16 99 // some irrelevant event
17 100 // <-- duplicate event: bad
18 100 // <-- duplicate event: bad again.
19 101
Run Code Online (Sandbox Code Playgroud)
我遗漏了DATETIME专栏.
如何找到#15和#17,考虑到开始不应连续出现两次?
结果应采用以下形式:
EVENT_ID FROM_ID FROM_DATETIME UPTO_ID UPTO_DATETIME
100 15 2014-01-01 14:00 17 2014-01-01 16:00
Run Code Online (Sandbox Code Playgroud)
我想它可以这样工作:
- 只能查找DATETIME订购的事件100,101的子选择.
- 按EVENT_ID分组以查找重复使用
..但count()
我怀疑这可能会导致结果崩溃,因为组会忽略排序,而且
我不知道如何获得两个(或更多!)相应的DATETIME值.
有可能在MySQL中找到它吗?我可以在PHP中处理这个问题,但我宁愿避免这种情况.
在我的示例中,出于方便起见,所有行都具有相同的日期时间值。
DROP TABLE IF EXISTS T;
CREATE TABLE T
(`ID` int auto_increment primary key, `EVENT_ID` int, dt timestamp)
;
INSERT INTO T
(`ID`, `EVENT_ID`)
VALUES
(10, 100),
(11, 101),
(12, 99),
(13, 100),
(14, 101),
(15, 100),
(16, 99),
(17, 100),
(18, 100),
(19, 101)
;
select id, event_id, dt as up_to_dt, prevgood, prevdt as from_dt from
(
select
T.*
, if((@startstop = 100 and event_id = 101) or (@startstop = 101 and event_id = 100), 'good', 'bad') as goodbad
, @prevgood := if(if((@startstop = 100 and event_id = 101) or (@startstop = 101 and event_id = 100), 'good', 'bad') = 'bad', @prevgood, id) as prevgood
, @prevdt := if(if((@startstop = 100 and event_id = 101) or (@startstop = 101 and event_id = 100), 'good', 'bad') = 'bad', @prevdt, dt) as prevdt
, @startstop := event_id
from
T, (select @startstop:=101, @prevgood:=0, @prevdt:=0) vars
where event_id in (100, 101)
order by id
) sq
where goodbad = 'bad';
Run Code Online (Sandbox Code Playgroud)
退货
+----+----------+---------------------+----------+---------------------+
| id | event_id | up_to_dt | prevgood | from_dt |
+----+----------+---------------------+----------+---------------------+
| 17 | 100 | 2014-01-20 09:12:20 | 15 | 2014-01-20 09:12:20 |
| 18 | 100 | 2014-01-20 09:12:20 | 15 | 2014-01-20 09:12:20 |
+----+----------+---------------------+----------+---------------------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
108 次 |
| 最近记录: |