加载中...
601-体育馆的人流量(Human Traffic of Stadium)
发表于:2021-12-03 | 分类: 困难
字数统计: 4.4k | 阅读时长: 26分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/human-traffic-of-stadium

英文原文

Table: Stadium

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| visit_date    | date    |
| people        | int     |
+---------------+---------+
visit_date is the primary key for this table.
Each row of this table contains the visit date and visit id to the stadium with the number of people during the visit.
No two rows will have the same visit_date, and as the id increases, the dates increase as well.

 

Write an SQL query to display the records with three or more rows with consecutive id's, and the number of people is greater than or equal to 100 for each.

Return the result table ordered by visit_date in ascending order.

The query result format is in the following example.

 

Example 1:

Input: 
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       |
+------+------------+-----------+
Output: 
+------+------------+-----------+
| id   | visit_date | people    |
+------+------------+-----------+
| 5    | 2017-01-05 | 145       |
| 6    | 2017-01-06 | 1455      |
| 7    | 2017-01-07 | 199       |
| 8    | 2017-01-09 | 188       |
+------+------------+-----------+
Explanation: 
The four rows with ids 5, 6, 7, and 8 have consecutive ids and each of them has >= 100 people attended. Note that row 8 was included even though the visit_date was not the next day after row 7.
The rows with ids 2 and 3 are not included because we need at least three consecutive ids.

中文题目

表:Stadium
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| visit_date    | date    |
| people        | int     |
+---------------+---------+
visit_date 是表的主键
每日人流量信息被记录在这三列信息中:序号 (id)、日期 (visit_date)、 人流量 (people)
每天只有一行记录,日期随着 id 的增加而增加

 

编写一个 SQL 查询以找出每行的人数大于或等于 100id 连续的三行或更多行记录。

返回按 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 连续的记录。

通过代码

官方题解

方法:使用 JOINWHERE 子句【通过】

思路

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

算法

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

[snippet1-MySQL]
select distinct t1.* from stadium t1, stadium t2, stadium t3 where t1.people >= 100 and t2.people >= 100 and t3.people >= 100 ;
| id | date       | people | id | date       | people | id | date       | people |
|----|------------|--------|----|------------|--------|----|------------|--------|
| 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    |
| 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    |
| 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    |
| 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    |
| 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    |
| 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    |
| 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    |
| 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    |
| 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    |
| 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    |
| 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    |
| 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    |
| 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    |
| 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    |
| 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    |
| 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    |
| 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    |
| 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    |
| 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    |
| 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    |
| 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    |
| 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    |
| 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    |
| 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    |
| 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    |
| 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    |
| 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    |
| 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    |
| 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    |
| 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    |
| 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    |
| 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    |
| 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    |
| 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    |
| 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    |
| 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    |
| 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    |
| 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    |
| 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    |
| 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    |
| 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    |
| 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    |
| 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    |
| 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    |
| 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    |
| 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    |
| 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    |
| 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    |
| 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    |
| 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    |
| 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    |
| 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    |
| 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    |
| 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    |
| 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    |
| 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    |
| 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    |
| 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    |
| 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    |
| 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    |
| 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    |
| 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    |
| 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    |
| 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    |
| 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    |
| 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    |
| 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    |
| 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    |
| 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    |
| 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    |
| 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    |
| 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    |
| 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    |
| 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    |
| 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    |
| 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    |
| 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    |
| 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    |
| 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    |
| 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    |
| 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    |
| 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    |
| 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    |
| 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    |
| 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    |
| 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    |
| 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    |
| 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    |
| 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    |
| 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    |
| 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    |
| 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    |
| 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    |
| 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    |
| 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    |
| 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    |
| 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    |
| 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    |
| 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    |
| 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    |
| 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    |
| 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    |
| 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    |
| 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    |
| 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    |
| 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    |
| 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    |
| 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    |
| 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   |
| 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   |
| 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   |
| 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   |
| 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   |
| 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   |
| 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   |
| 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   |
| 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   |
| 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   |
| 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   |
| 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   |
| 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   |
| 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   |
| 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   |
| 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   |
| 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   |
| 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   |
| 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   |
| 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   |
| 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   |
| 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   |
| 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   |
| 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   |
| 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   |
| 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   |
| 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   |
| 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   |
| 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   |
| 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   |
| 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   |
| 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   |
| 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   |
| 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   |
| 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   |
| 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   |
| 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    |
| 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    |
| 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    |
| 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    |
| 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    |
| 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    |
| 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    |
| 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    |
| 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    |
| 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    |
| 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    |
| 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    |
| 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    |
| 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    |
| 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    |
| 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    |
| 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    |
| 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    |
| 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    |
| 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    |
| 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    |
| 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    |
| 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    |
| 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    |
| 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    |
| 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    |
| 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    |
| 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    |
| 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    |
| 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    |
| 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    |
| 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    |
| 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    |
| 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    |
| 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    |
| 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    |
| 2  | 2017-01-02 | 109    | 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    |
| 3  | 2017-01-03 | 150    | 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    |
| 5  | 2017-01-05 | 145    | 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    |
| 6  | 2017-01-06 | 1455   | 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    |
| 7  | 2017-01-07 | 199    | 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    |
| 8  | 2017-01-08 | 188    | 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    |
| 2  | 2017-01-02 | 109    | 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    |
| 3  | 2017-01-03 | 150    | 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    |
| 5  | 2017-01-05 | 145    | 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    |
| 6  | 2017-01-06 | 1455   | 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    |
| 7  | 2017-01-07 | 199    | 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    |
| 8  | 2017-01-08 | 188    | 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    |
| 2  | 2017-01-02 | 109    | 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    |
| 3  | 2017-01-03 | 150    | 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    |
| 5  | 2017-01-05 | 145    | 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    |
| 6  | 2017-01-06 | 1455   | 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    |
| 7  | 2017-01-07 | 199    | 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    |
| 8  | 2017-01-08 | 188    | 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    |
| 2  | 2017-01-02 | 109    | 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    |
| 3  | 2017-01-03 | 150    | 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    |
| 5  | 2017-01-05 | 145    | 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    |
| 6  | 2017-01-06 | 1455   | 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    |
| 7  | 2017-01-07 | 199    | 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    |
| 8  | 2017-01-08 | 188    | 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    |
| 2  | 2017-01-02 | 109    | 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    |
| 3  | 2017-01-03 | 150    | 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    |
| 5  | 2017-01-05 | 145    | 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    |
| 6  | 2017-01-06 | 1455   | 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    |
| 7  | 2017-01-07 | 199    | 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    |
| 8  | 2017-01-08 | 188    | 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    |
| 2  | 2017-01-02 | 109    | 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    |
| 3  | 2017-01-03 | 150    | 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    |
| 5  | 2017-01-05 | 145    | 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    |
| 6  | 2017-01-06 | 1455   | 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    |
| 7  | 2017-01-07 | 199    | 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    |
| 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    | 8  | 2017-01-08 | 188    |
  • 共有 6 天人流量超过 100 人,笛卡尔积 后有 216(666) 条记录。
  • 前 3 列来自表 t1,中间 3 列来自表 t2,最后 3 列来自表 t3。

表 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
[snippet2-MySQL]
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 | date       | people |
|----|------------|--------|
| 7  | 2017-01-07 | 199    |
| 6  | 2017-01-06 | 1455   |
| 8  | 2017-01-08 | 188    |
| 7  | 2017-01-07 | 199    |
| 5  | 2017-01-05 | 145    |
| 6  | 2017-01-06 | 1455   |

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

MySQL

[solution1-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 ;

统计信息

通过次数 提交次数 AC比率
33421 66295 50.4%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
600-不含连续1的非负整数(Non-negative Integers without Consecutive Ones)
下一篇:
605-种花问题(Can Place Flowers)
本文目录
本文目录