我有一张这样的桌子:
CREATE TABLE `hoststatus` (
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`host` varchar(100) NOT NULL,
`result` tinyint(1) NOT NULL,
PRIMARY KEY (`timestamp`,`host`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `hoststatus` (`timestamp`, `host`, `result`) VALUES
('2012-06-08 14:18:01', 'host1', 0),
('2012-06-08 14:19:01', 'host1', 0),
('2012-06-08 14:20:01', 'host1', 1),
('2012-06-08 14:21:01', 'host1', 1),
('2012-06-08 14:22:01', 'host1', 1),
('2012-06-08 14:23:01', 'host1', 1),
('2012-06-08 14:24:01', 'host1', 0),
('2012-06-08 14:25:01', 'host1', 0),
('2012-06-08 14:26:01', 'host1', 0),
('2012-06-08 14:27:01', 'host1', 0),
('2012-06-08 14:28:01', 'host1', 1),
('2012-06-08 14:29:01', 'host1', 1),
('2012-06-08 14:29:04', 'host2', 0),
('2012-06-08 14:30:01', 'host1', 0),
('2012-06-08 14:30:03', 'host2', 0),
('2012-06-08 14:31:01', 'host1', 0),
('2012-06-08 14:32:01', 'host1', 0),
('2012-06-08 14:32:02', 'host2', 1),
('2012-06-08 14:33:01', 'host1', 0);
这包含一些主机的连接状态:1 表示“离线”,0 表示“在线”。我想使用选择请求推断的是每个主机的每个“离线”状态的开始和结束时间戳。在上面的示例中,请求应返回如下内容:
begin end host
'2012-06-08 14:20:01' '2012-06-08 14:23:01' 'host1'
'2012-06-08 14:28:01' '2012-06-08 14:29:01' 'host1'
'2012-06-08 14:32:02' '2012-06-08 14:32:02' 'host2'
有人可以给我解决这个问题的方法吗?谢谢你。