MySQL体育馆的人流量

Posted willem_chen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL体育馆的人流量相关的知识,希望对你有一定的参考价值。

SQL架构

Create table If Not Exists stadium (id int, visit_date DATE NULL, people int);
Truncate table stadium

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')

表: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 连续的记录。

正确答案

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;

题解

方法:使用 JOIN 和 WHERE 子句【通过】

思路

在表 stadium 中查询人流量超过 100 的记录,将查询结果与其自身的临时表连接,再使用 WHERE 子句获得满足条件的记录。

算法

第一步:查询人流量超过 100 的记录,然后将结果与其自身的临时表连接。

select distinct t1.*
from stadium t1, stadium t2, stadium t3
where t1.people >= 100 and t2.people >= 100 and t3.people >= 100;

共有 6 天人流量超过 100 人,笛卡尔积 后有 216(666) 条记录。
前 3 列来自表 t1,中间 3 列来自表 t2,最后 3 列来自表 t3。

mysql> select distinct t1.* from stadium t1, stadium t2, stadium t3 where t1.people >= 100 and t2.people >= 100 and t3.people >= 100;
+------+------------+--------+
| id   | visit_date | people |
+------+------------+--------+
|    2 | 2017-01-02 |    109 |
|    3 | 2017-01-03 |    150 |
|    5 | 2017-01-05 |    145 |
|    6 | 2017-01-06 |   1455 |
|    7 | 2017-01-07 |    199 |
|    8 | 2017-01-09 |    188 |
+------+------------+--------+
6 rows in set (0.01 sec)

表 t1,t2 和 t3 相同,需要考虑添加哪些条件能够得到想要的结果。以 t1 为例,它有可能是高峰期的第 1 天,第 2 天,或第 3 天。

t1 是高峰期第 1 天:(t1.id - t2.id = 1 and t1.id - t3.id = 2 and t2.id - t3.id =1) – t1, t2, t3
t1 是高峰期第 2 天:(t2.id - t1.id = 1 and t2.id - t3.id = 2 and t1.id - t3.id =1) – t2, t1, t3
t1 是高峰期第 3 天:(t3.id - t2.id = 1 and t2.id - t1.id =1 and t3.id - t1.id = 2) – t3, t2, t1

select 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
);
+------+------------+--------+
| id   | visit_date | people |
+------+------------+--------+
|    7 | 2017-01-07 |    199 |
|    6 | 2017-01-06 |   1455 |
|    8 | 2017-01-09 |    188 |
|    7 | 2017-01-07 |    199 |
|    5 | 2017-01-05 |    145 |
|    6 | 2017-01-06 |   1455 |
+------+------------+--------+
6 rows in set (0.00 sec)

可以看到查询结果中存在重复的记录,再使用 DISTINCT 去重。

mysql> 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)

答案2

select DISTINCT t1.* from stadium t1, stadium t2, stadium t3 
where 
t1.people >= 100 and 
t2.people >= 100 and 
t3.people >= 100 and ( 
(t2.id-t1.id=1 and t3.id-t2.id=1 ) or 
(t1.id-t2.id=1 and t3.id-t1.id=1 ) or 
(t1.id-t2.id=1 and t2.id-t3.id=1 ) ) 
ORDER BY t1. visit_date;

+------+------------+--------+
| 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体育馆的人流量的主要内容,如果未能解决你的问题,请参考以下文章

MySQL体育馆的人流量

MySQL体育馆的人流量

LC-601 体育馆人流量

力扣——体育馆的人流量

601. 体育馆的人流量

LeetCode(数据库)- 体育馆的人流量