我有GPS跟踪器的数据。 像这样说:
CREATE TABLE IF NOT EXISTS `tab_gps` (
`id` int(6) unsigned NOT NULL,
`speed` int(3) unsigned NOT NULL,
`time` time NOT NULL,
`tracker_name` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
和一些示例数据:
('1', '5', '07:00', 'car'),
('2', '10', '07:10', 'car'),
('3', '0', '07:20', 'bus'),
('4', '0', '07:30', 'car'),
('5', '0', '07:40', 'car'),
('6', '0', '07:50', 'car'),
('7', '20', '08:00', 'car'),
('8', '40', '08:10', 'bus'),
('9', '15', '08:15', 'car'),
('10', '0', '08:32', 'car'),
('11', '0', '08:40', 'car'),
('12', '0', '08:52', 'bus'),
('13', '12', '09:10', 'car'),
('14', '0', '09:25', 'car'),
('15', '0', '09:30', 'car'),
('16', '0', '09:35', 'car'),
('17', '10', '09:41', 'car'),
('18', '5', '09:46', 'car');
('19', '0', '09:50', 'car');
问题是如何查找特定“ tracker_name”开始其路线的时间
所以在我的示例算法中,我的头脑是:
在我的示例中,该职位是时间:09:41 (id = 17)
我正在尝试使用:
select max(time) from tab_gps where tracker_name='car' and speed <>0
这里是让您更好理解的小提琴:http://sqlfiddle.com/#!9/834371
答案 0 :(得分:-1)
好吧,对于最终路线,您可以执行以下操作:
select g.*
from tab_gp2 g
where g.tracker_name = 'car' and
g.id > (select coalesce(max(g2.id), -1)
from tab_gps g2
where g2.speed = 0 and g2.tracker_name = 'car'
);
Here是db <>提琴,说明了逻辑。