X 市建了一個新的體育館,每日人流量信息被記錄在這三列信息中:序號 (id)、日期 (visit_date)、?人流量 (people)。
請編寫一個查詢語句,找出人流量的高峰期。高峰期時,至少連續三行記錄中的人流量不少于100。
例如,表 stadium:
+------+------------+-----------+
| id ? | visit_date | people ? ?|
+------+------------+-----------+
| 1 ? ?| 2017-01-01 | 10 ? ? ? ?|
| 2 ? ?| 2017-01-02 | 109 ? ? ? |
| 3 ? ?| 2017-01-03 | 150 ? ? ? |
| 4 ? ?| 2017-01-04 | 99 ? ? ? ?|
| 5 ? ?| 2017-01-05 | 145 ? ? ? |
| 6 ? ?| 2017-01-06 | 1455 ? ? ?|
| 7 ? ?| 2017-01-07 | 199 ? ? ? |
| 8 ? ?| 2017-01-08 | 188 ? ? ? |
+------+------------+-----------+
對于上面的示例數據,輸出為:
+------+------------+-----------+
| id ? | visit_date | people ? ?|
+------+------------+-----------+
| 5 ? ?| 2017-01-05 | 145 ? ? ? |
| 6 ? ?| 2017-01-06 | 1455 ? ? ?|
| 7 ? ?| 2017-01-07 | 199 ? ? ? |
| 8 ? ?| 2017-01-08 | 188 ? ? ? |
+------+------------+-----------+
?
提示:
每天只有一行記錄,日期隨著 id 的增加而增加。
思路:三個自連接,把三種情況寫一下,死亡大SQL
select distinct t1.*
from stadium t1, stadium t2, stadium t3
where t1.people >= 100 and t2.people >= 100 and t3.people >= 100
and
((t1.id - t2.id = 1 and t2.id - t3.id =1)or(t1.id - t3.id =1 and t2.id - t1.id = 1)or(t3.id - t2.id = 1 and t2.id - t1.id =1)
)
order by t1.id;
?