说是困难,但是实际写起来还是很简单的。
难点在于【三条连续记录满足条件】上,不经常写sql的可能一下子反应不过来,其实就是三个stadium表内联,查每条数据的时候,看看其他两个表中是否能找到满足条件的数据,如下所示。其中用加号和等号而不是大于小于号是为了用上索引(如果有的话),三个>=100放最后是为了让解释器优先处理,group by 是为了去重,也可以换成distinct或union.
select t1.id,t1.visit_date,t1.people
from stadium t1,stadium t2,stadium t3
where 1=1
and ((t1.id = t2.id + 1
and t3.id = t1.id + 1)
or (t1.id = t2.id + 1
and t1.id = t3.id + 2)
or (t2.id = t1.id + 1
and t3.id = t1.id + 2))
and t1.people >= 100
and t2.people >= 100
and t3.people >= 100
group by t1.id,t1.visit_date,t1.people
order by t1.id