LeetCode(数据库)- 体育馆的人流量
Posted Lux_Sun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- 体育馆的人流量相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:略。
解题思路:
- 解决方案(1)
第一步:查询人流量超过 100 的记录,然后将结果与其自身的临时表连接。
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
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
去重。
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;
- 解答疑惑
解惑1:高峰顺序貌似搞反了吧,应该是以下写法才说得通。不过不影响执行的结果。而且不需要 ORDER BY,但也不能说上面写错了,因为人家倒过来想,只要最后 ORDER BY 下即可~
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
解惑2:为啥是这 3 种情况,而不是 6 种情况排列组合呢?比如:有 t1,t2,t3,为啥没有 t1,t3,t2 的情况呢?
其实本质是一样的 t1,t2,t3 和 t1,t3,t2 结果肯定是一样的,多此一举罢了。但是上面这 3 种分别都是不一样的,本质不一样,自己感觉下!
解惑3:题目不是说大于等于 3 天,那假如是连续 4 天呢,第 4 天的判断为何没有?
其实仔细想下上面的公式,一个连续 4 天,能不能看成 2 个连续的 3 天组成,懂了吧!
- 解决方案(2) (mysql 8.0+)
使用 row_number() over() 函数 + with as 临时表语法
with t1 as(
select *,id - row_number() over(order by id) as rk
from stadium
where people >= 100
)
select id,visit_date,people
from t1
where rk in(
select rk
from t1
group by rk
having count(rk) >= 3
)
通过 row_number() over() 排名 1...N,从 id - 排名得最终连续的规律,牛皮~
- 解决方案(3) (MySQL 8.0+)
使用自定义变量做标记 + with as 临时表语法
WITH t2 AS(
WITH t1 AS(
SELECT id, visit_date, people FROM Stadium WHERE people >= 100
)
SELECT id, visit_date, people, @gid:=IF(id - @preId = 1, @gid, @gid+1) groupId, @preId:=id
FROM t1, (SELECT @preId:=(SELECT id FROM t1 LIMIT 0, 1), @gid:=0) init
)
SELECT id, visit_date, people
FROM t2
WHERE groupId IN (
SELECT groupId
FROM t2
GROUP BY groupId
HAVING COUNT(groupId) >= 3
)
下面查询思路和(2)都一样,关键在于上面 with as 部分:这边思路是从第一条开始做标记数字1,如果中间断了不连续的话,就 +1,连续则继续复用上次的标记数值,最终效果是和(2)一样的如上图所示。
注意:这里的临时表有 2 个,不能并级写,只能嵌套的写法。
// 反例: 运行错误
WITH t1 AS(
SELECT id, visit_date, people FROM Stadium WHERE people >= 100
)
WITH t2 AS(
SELECT id, visit_date, people, @gid:=IF(id - @preId = 1, @gid, @gid+1) groupId, @preId:=id
FROM t1, (SELECT @preId:=(SELECT id FROM t1 LIMIT 0, 1), @gid:=0) init
)
以上是关于LeetCode(数据库)- 体育馆的人流量的主要内容,如果未能解决你的问题,请参考以下文章