MySQL数据库 - 复杂查询(一)第四关
·

@R星校长
第4关:体育馆的人流量
本关任务:某市建了一个新的体育馆,每日人流量信息被记录在gymnasium表中:序号 (id)、日期 (date)、 人流量 (visitors_flow)。
请编写一个查询语句,找出人流量的高峰期。高峰期时,至少连续三行记录中的人流量不少于100。
gymnasium表结构数据如下:
| id | date | visitors_flow |
|---|---|---|
| 1 | 2019-01-01 | 58 |
| 2 | 2019-01-02 | 110 |
| 3 | 2019-01-03 | 123 |
| 4 | 2019-01-04 | 67 |
| 5 | 2019-01-05 | 168 |
| 6 | 2019-01-06 | 1352 |
| 7 | 2019-01-07 | 382 |
| 8 | 2019-01-08 | 326 |
| 9 | 2019-01-09 | 99 |
提示:每天只有一行记录,日期随着id的增加而增加。
预期输出:
+----+------------+---------------+
| id | date | visitors_flow |
+----+------------+---------------+
| 5 | 2019-01-05 | 168 |
| 6 | 2019-01-06 | 1352 |
| 7 | 2019-01-07 | 382 |
| 8 | 2019-01-08 | 326 |
+----+------------+---------------+
开始你的任务吧,祝你成功!

答案:
#请在此添加实现代码
########## Begin ##########
#方法一
select * from gymnasium where exists (
select * from (
select a.date a1,b.date b1,c.date c1 from
(select * from gymnasium where visitors_flow >=100) a
inner join
(select * from gymnasium where visitors_flow >=100) b
on b.id-a.id=1
inner join
(select * from gymnasium where visitors_flow >=100) c
on c.id-a.id=2 ) as tb_date
where gymnasium.date in (a1,b1,c1)
);
#方法二
select distinct a.* from gymnasium a,gymnasium b,gymnasium c
where a.visitors_flow>=100 and b.visitors_flow>=100 and c.visitors_flow>=100
and (
(a.id = b.id-1 and b.id = c.id -1) or
(a.id = b.id-1 and a.id = c.id +1) or
(a.id = b.id+1 and b.id = c.id +1)
) order by a.id;
########## End ##########
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐



所有评论(0)