# 图解面试题：找出连续出现 N 次的内容？

![](https://4109408004-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MTuZ3PxkXf2a7UbRybW%2F-MV5i4Jr9HU-A_HhDEuO%2F-MV5i8nFKSdYElWlTTGJ%2F%E6%89%BE%E5%88%B0%E8%BF%9E%E7%BB%AD%E5%87%BA%E7%8E%B0N%E6%AC%A1%E7%9A%84%E5%86%85%E5%AE%B9_1.webp?alt=media\&token=3c64071d-7b41-4c73-968b-ae1b0cd1f6d4)

**【题目】**

下面是学生的成绩表（表名 score，列名：学号、成绩），使用 SQL 查找所有至少连续出现 3 次的成绩。

![](https://4109408004-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MTuZ3PxkXf2a7UbRybW%2F-MV5i4Jr9HU-A_HhDEuO%2F-MV5iD_oPjtkzzvrcOaK%2F%E6%89%BE%E5%88%B0%E8%BF%9E%E7%BB%AD%E5%87%BA%E7%8E%B0N%E6%AC%A1%E7%9A%84%E5%86%85%E5%AE%B9_2.png?alt=media\&token=ee307687-1576-4aa3-b71a-1c93c7bd8de9)

例如，“成绩” 这一列里 84 是连续出现 3 次的成绩。

**【解题思路】**

1.条件 1：什么是连续出现 3 次？

假设 “学号” 是按顺序排列的（如果不是，可以增加一列，这一列是按序号顺序排列的），所以每一学号与上一学号相差 1。例如下图的 3 个学号是连续学号，他们之间的关系是：

某一学号（0002）= 下一位的学号（0003）-1

下一位学号（0003）= 下下位学号（0004）-1

![](https://4109408004-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MTuZ3PxkXf2a7UbRybW%2F-MV5i4Jr9HU-A_HhDEuO%2F-MV5iKeKk-xcC3kQkw-x%2F%E6%89%BE%E5%88%B0%E8%BF%9E%E7%BB%AD%E5%87%BA%E7%8E%B0N%E6%AC%A1%E7%9A%84%E5%86%85%E5%AE%B9_3.webp?alt=media\&token=c805150b-1e6a-4536-9ffd-78b2d3f5d11a)

2.条件 2：成绩相等

如果这 3 个连续学号的**成绩相等**，就是题目要求的 “至少连续出现 3 次的成绩”。

3.利用 “自关联 “的思路

自连接（自身连接）的本质是把一张表复制出多张一模一样的表来使用。SQL 语法：

```sql
select 列名
from 表名 as 别名1,表名 as 别名2;
```

步骤 1）将成绩表（score）复制 3 个一样的表，分别命名为 a、b、c

```sql
select *
from score as a,
   score as b,
   score as c;
```

![](https://4109408004-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MTuZ3PxkXf2a7UbRybW%2F-MV5i4Jr9HU-A_HhDEuO%2F-MV5iUa6l-xLfIOj4i66%2F%E6%89%BE%E5%88%B0%E8%BF%9E%E7%BB%AD%E5%87%BA%E7%8E%B0N%E6%AC%A1%E7%9A%84%E5%86%85%E5%AE%B9_4.webp?alt=media\&token=957ec31e-527a-459b-899a-23a53ac1d500)

步骤 2）我们需要找到这 3 个表中 3 个**连续**的学号，这个条件如下

a. 学号 = b. 学号 - 1 and b. 学号 = c. 学号 - 1

![](https://4109408004-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MTuZ3PxkXf2a7UbRybW%2F-MV5i4Jr9HU-A_HhDEuO%2F-MV5iX8a4e-fQOAu08U9%2F%E6%89%BE%E5%88%B0%E8%BF%9E%E7%BB%AD%E5%87%BA%E7%8E%B0N%E6%AC%A1%E7%9A%84%E5%86%85%E5%AE%B9_5.webp?alt=media\&token=00aa1f06-b8a7-4151-bb5b-42bb3836f684)

步骤 3）还要让这 3 个学号连续的人 “**成绩相等**”，这个条件如下

a. 成绩 = b. 成绩 and b. 成绩 = c. 成绩

将步骤 2 和步骤 3 的条件合并起来就是下面 SQL 里的 where 字句：

```sql
select *
from score as a,
   score as b,
   score as c;
 where a.学号 = b.学号 - 1
   and b.学号 = c.学号 - 1
   and a.成绩 = b.成绩
   and b.成绩 = c.成绩;
```

步骤 4）前面步骤已经将连续 3 人相等的成绩找出，现在用 distinct 去掉自连接产生的重复数。最终 SQL 如下：

```sql
select distinct a.成绩 as 最终答案
from score as a,
   score as b,
   score as c;
 where a.学号 = b.学号 - 1
   and b.学号 = c.学号 - 1
   and a.成绩 = b.成绩
   and b.成绩 = c.成绩;
```

最终结果：

![](https://4109408004-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MTuZ3PxkXf2a7UbRybW%2F-MV5i4Jr9HU-A_HhDEuO%2F-MV5idpHWQmvElM9nPPl%2F%E6%89%BE%E5%88%B0%E8%BF%9E%E7%BB%AD%E5%87%BA%E7%8E%B0N%E6%AC%A1%E7%9A%84%E5%86%85%E5%AE%B9_6.png?alt=media\&token=ac9ac17f-cb68-4603-9939-f3492997df90)

**【本题考点】**

• 本题考察的是连续出现，会有同学忽略 “连续” 二字

• 考察对自关联的灵活应用

• 从题目连续 3 次成绩相等，判断出 “成绩相等” 和“学号连续”这 2 个条件。考察构建 “连续学号成绩相等” 的思维构建能力

**【举一反三】**

遇到类似 “连续出 N 次的问题” 可以回想本题的解答思路，如：查询至少连续 3 天没有出勤的员工。

推荐：[如何提升你的分析技能，实现升职加薪？](http://mp.weixin.qq.com/s?__biz=MzAxMTMwNTMxMQ==\&mid=2649246542\&idx=2\&sn=522f75638ff0e49fb23c1c74b89e3db9\&chksm=835fc37eb4284a68e2f1d31d53323e37f36f2637070aaf594edabbb376a0416d09315b9d0bee\&scene=21#wechat_redirect)

![](https://4109408004-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MTuZ3PxkXf2a7UbRybW%2F-MV5i4Jr9HU-A_HhDEuO%2F-MV5ihdOHed9inj-pr7h%2F%E6%89%BE%E5%88%B0%E8%BF%9E%E7%BB%AD%E5%87%BA%E7%8E%B0N%E6%AC%A1%E7%9A%84%E5%86%85%E5%AE%B9_7.webp?alt=media\&token=2b1875d0-3f4b-467b-934b-0f4488fd0c3d)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://houzidata.gitbook.io/sql/di-3-zhang-duo-biao-cha-xun/tu-jie-mian-shi-ti-zhao-chu-lian-xu-chu-xian-n-ci-de-nei-rong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
