MySQL找到行

时间:2014-01-08 11:00:06

标签: php mysql

假设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

我遗漏了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

我想它可以像这样工作:
- 一个子选择,仅查找DATETIME订购的事件100,101 - 按EVENT_ID分组,使用count()来查找重复项 ..但我怀疑这会导致结果崩溃,因为小组会忽略排序 ..并且我不知道如何获得两个(或更多!)相应的DATETIME值。

有可能在MySQL中找到它吗?我可以在PHP中处理这个问题,但我宁愿避免这种情况。

1 个答案:

答案 0 :(得分:3)

在我的示例中,所有行都具有相同的日期时间值。

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';

返回

+----+----------+---------------------+----------+---------------------+
| 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 |
+----+----------+---------------------+----------+---------------------+