描述
请编写一个 SQL 语句,在球员表(players
)中找出至少有三个 id 连续的球员达到此高度的球员身高,返回的结果可以按任意顺序排列。
表定义: players
(球员表)
列名 | 类型 | 注释 |
---|---|---|
id | int unsigned | 主键 |
height | int | 球员身高 |
【附答案】花高价求来的OpenAI题库,仅限前50份!
微信加sunny【jiuzhang1104】备注【AI】即可领取
返回的结果可以按任意顺序排列
样例
样例一:
表内容: players
id | height |
---|---|
1 | 198 |
2 | 198 |
3 | 198 |
4 | 226 |
如上述球员 (players
) 表,id 为 1、2、3 的球员身高值都达到了 198,所以返回 198
height |
---|
198 |
样例二:
表内容: players
id | height |
---|---|
1 | 198 |
2 | 196 |
3 | 188 |
如上述球员 (players
) 表,没有满足条件的身高,则什么也不返回
思路: 像这种在表中连续的查询的,可以使用自连接的方式来解决。
将表players与自己连接两次,
得到一个三表连接的结果集,然后筛选出三个表中 id
连续的记录,查看记录中三个表的身高 height
是否相等
题解代码:
select p1.height height
from players p1, players p2, players p3
where p2.id = p1.id +1
and p3.id = p2.id +1
and p2.height = p1.height and p3.height = p2.height