★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-zspkpwaf-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
SQL架構git
1 Create table If Not Exists stadium (id int, visit_date DATE NULL, people int) 2 Truncate table stadium 3 insert into stadium (id, visit_date, people) values ('1', '2017-01-01', '10') 4 insert into stadium (id, visit_date, people) values ('2', '2017-01-02', '109') 5 insert into stadium (id, visit_date, people) values ('3', '2017-01-03', '150') 6 insert into stadium (id, visit_date, people) values ('4', '2017-01-04', '99') 7 insert into stadium (id, visit_date, people) values ('5', '2017-01-05', '145') 8 insert into stadium (id, visit_date, people) values ('6', '2017-01-06', '1455') 9 insert into stadium (id, visit_date, people) values ('7', '2017-01-07', '199') 10 insert into stadium (id, visit_date, people) values ('8', '2017-01-08', '188')
X city built a new stadium, each day many people visit it and the stats are saved as these columns: id, visit_date, peoplegithub
Please write a query to display the records which have 3 or more consecutive rows and the amount of people more than 100(inclusive).sql
For example, the table 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 | +------+------------+-----------+
For the sample data above, the output is:架構
+------+------------+-----------+ | id | visit_date | people | +------+------------+-----------+ | 5 | 2017-01-05 | 145 | | 6 | 2017-01-06 | 1455 | | 7 | 2017-01-07 | 199 | | 8 | 2017-01-08 | 188 | +------+------------+-----------+
Note:
Each day only have one row record, and the dates are increasing with id increasing.ui
X 市建了一個新的體育館,每日人流量信息被記錄在這三列信息中:序號 (id)、日期 (date)、 人流量 (people)。spa
請編寫一個查詢語句,找出高峯期時段,要求連續三天及以上,而且天天人流量均很多於100。code
例如,表 stadium
:htm
+------+------------+-----------+ | id | 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 | date | people | +------+------------+-----------+ | 5 | 2017-01-05 | 145 | | 6 | 2017-01-06 | 1455 | | 7 | 2017-01-07 | 199 | | 8 | 2017-01-08 | 188 | +------+------------+-----------+
Note:
天天只有一行記錄,日期隨着 id 的增長而增長。
260ms
1 # Write your MySQL query statement below 2 select distinct t1.* 3 from stadium t1, stadium t2, stadium t3 4 where t1.people>=100 and t2.people>=100 and t3.people>=100 5 and 6 ((t1.id-t2.id=1 and t1.id-t3.id=2 and t2.id-t3.id=1) 7 or 8 (t2.id-t1.id=1 and t2.id-t3.id=2 and t1.id-t3.id=1) 9 or 10 (t3.id-t2.id=1 and t2.id-t1.id=1 and t3.id-t1.id=2)) 11 order by t1.id
266ms
1 # Write your MySQL query statement below 2 3 select distinct t.* from 4 stadium t , 5 ( 6 select 7 t1.Id, t2.Id as t2Id, t3.Id as t3Id 8 from ( 9 select * from stadium 10 where people>=100) as t1 11 left join 12 ( 13 select * from stadium 14 where people>=100 ) as t2 15 ON t1.Id+1=t2.Id 16 left join 17 ( 18 select * from stadium 19 where people>=100 ) as t3 20 ON t1.Id+2=t3.Id 21 where (t2.Id is not null and t3.Id is not null) 22 ) as b 23 where (t.Id=b.Id or t.Id=b.t2Id or t.Id=b.t3Id);
268ms
1 # Write your MySQL query statement below 2 SELECT s1.id AS id 3 ,s1.date AS date 4 ,s1.people AS people 5 FROM stadium s1, stadium s2, stadium s3 6 WHERE( 7 (s1.id = s2.id - 1 AND s1.id = s3.id - 2) 8 OR (s1.id = s2.id + 1 AND s1.id = s3.id - 1) 9 OR (s1.id = s2.id + 1 AND s1.id = s3.id +2) 10 ) 11 AND s1.people >= 100 12 AND s2.people >= 100 13 AND s3.people >= 100 14 GROUP BY s1.id;