MySQL体育馆的人流量
Posted willem_chen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL体育馆的人流量相关的知识,希望对你有一定的参考价值。
SQL架构
Create table If Not Exists stadium (id int, visit_date DATE NULL, people int);
insert into stadium (id, visit_date, people) values ('1', '2017-01-01', '10');
insert into stadium (id, visit_date, people) values ('2', '2017-01-02', '109');
insert into stadium (id, visit_date, people) values ('3', '2017-01-03', '150');
insert into stadium (id, visit_date, people) values ('4', '2017-01-04', '99');
insert into stadium (id, visit_date, people) values ('5', '2017-01-05', '145');
insert into stadium (id, visit_date, people) values ('6', '2017-01-06', '1455');
insert into stadium (id, visit_date, people) values ('7', '2017-01-07', '199');
insert into stadium (id, visit_date, people) values ('8', '2017-01-09', '188');
mysql> SELECT * FROM `stadium` LIMIT 0, 1000;
+------+------------+--------+
| 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-09 | 188 |
+------+------------+--------+
8 rows in set (0.00 sec)
题目描述
表:Stadium
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| visit_date | date |
| people | int |
+---------------+---------+
visit_date 是表的主键
每日人流量信息被记录在这三列信息中:
- 序号 (id)、
- 日期 (visit_date)、
- 人流量 (people)
每天只有一行记录,日期随着 id 的增加而增加
编写一个 SQL 查询找出,每行的人数大于或等于 100 且 id 连续的三行或更多行记录。
返回按 visit_date 升序排列的结果表。
查询结果格式如下所示。
Stadium table:
+------+------------+-----------+
| 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-09 | 188 |
+------+------------+-----------+
Result table:
+------+------------+-----------+
| id | visit_date | people |
+------+------------+-----------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+-----------+
id 为 5、6、7、8 的四行 id 连续,并且每行都有 >= 100 的人数记录。
请注意,即使第 7 行和第 8 行的 visit_date 不是连续的,输出也应当包含第 8 行,因为我们只需要考虑 id 连续的记录。
不输出 id 为 2 和 3 的行,因为至少需要三条 id 连续的记录。
题解
答A
解题思路
先用一个变量cnt
记录连续不少于100人的情况,即当前一天及今天都超过99人时,cnt
加1。
select id,visit_date,people,(@cnt:=IF(people>99,@cnt+1,0)) as cnt from stadium;
+------+------------+--------+------+
| id | visit_date | people | cnt |
+------+------------+--------+------+
| 1 | 2017-01-01 | 10 | 0 |
| 2 | 2017-01-02 | 109 | 1 |
| 3 | 2017-01-03 | 150 | 2 |
| 4 | 2017-01-04 | 99 | 0 |
| 5 | 2017-01-05 | 145 | 1 |
| 6 | 2017-01-06 | 1455 | 2 |
| 7 | 2017-01-07 | 199 | 3 |
| 8 | 2017-01-09 | 188 | 4 |
+------+------------+--------+------+
8 rows in set (0.00 sec)
此时,我们只要取出cnt
大于 2 的数据及前 cnt-1
天的数据,由于id是连续的,可以取出当前id及前cnt-1
个id的数据,存在超过3天连续的情况,对取出的结果去重。
select distinct s.* from stadium s,
(
select id,visit_date,people,(@cnt:=IF(people>99,@cnt+1,0)) as cnt from stadium
) c where c.cnt>2 and s.id between c.id-c.cnt+1 and c.id;
+------+------------+--------+
| id | visit_date | people |
+------+------------+--------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+--------+
4 rows in set (0.00 sec)
答B
SELECT distinct a.*
FROM stadium as a,stadium as b,stadium as c
where ((a.id = b.id-1 and b.id+1 = c.id) or
(a.id-1 = b.id and a.id+1 = c.id) or
(a.id-1 = c.id and c.id-1 = b.id))
and (a.people>=100 and b.people>=100 and c.people>=100)
order by a.id;
+------+------------+--------+
| id | visit_date | people |
+------+------------+--------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+--------+
4 rows in set (0.00 sec)
答C
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 t1.id - t3.id = 2 and t2.id - t3.id =1) -- t1, t2, t3
or
(t2.id - t1.id = 1 and t2.id - t3.id = 2 and t1.id - t3.id =1) -- t2, t1, t3
or
(t3.id - t2.id = 1 and t2.id - t1.id =1 and t3.id - t1.id = 2) -- t3, t2, t1
)
order by t1.id;
+------+------------+--------+
| id | visit_date | people |
+------+------------+--------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+--------+
4 rows in set (0.00 sec)
以上是关于MySQL体育馆的人流量的主要内容,如果未能解决你的问题,请参考以下文章