如何选择那些记录?

时间:2014-05-29 07:44:17

标签: mysql sql

我有这样的表格,我想选择这些行:

enter image description here

所以如果连续速度= 0的记录超过3条,则选择它。

2 个答案:

答案 0 :(得分:1)

SELECT * 
FROM table_name tn
WHERE

((SELECT COUNT(1) FROM table_name WHERE id=tn.id+1 AND speed=0)>0 AND
(SELECT COUNT(1) FROM table_name WHERE id=tn.id+2 AND speed=0)>0) OR

((SELECT COUNT(1) FROM table_name WHERE id=tn.id-1 AND speed=0)>0 AND
(SELECT COUNT(1) FROM table_name WHERE id=tn.id-2 AND speed=0)>0) OR

((SELECT COUNT(1) FROM table_name WHERE id=tn.id+1 AND speed=0)>0 AND
(SELECT COUNT(1) FROM table_name WHERE id=tn.id-1 AND speed=0)>0)

答案 1 :(得分:0)

select *
from table_name
where speed=0
and (select count(*) from table_name where speed=0) > 3