实战MySQL——LeetCode数据库习题全解

一、简单

01 组合两个表

LeetCode175

题目描述

表1: Person

+-------------+---------+
| 列名         | 类型     |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+
PersonId 是上表主键

表2: Address

+-------------+---------+
| 列名         | 类型    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+
AddressId 是上表主键

编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:

FirstName, LastName, City, State

建表

DROP TABLE
IF
    EXISTS Person;
CREATE TABLE Person ( PersonId INT, FirstName VARCHAR ( 255 ), LastName VARCHAR ( 255 ) );
DROP TABLE
IF
    EXISTS Address;
CREATE TABLE Address ( AddressId INT, PersonId INT, City VARCHAR ( 255 ), State VARCHAR ( 255 ) );
INSERT INTO Person ( PersonId, LastName, FirstName )
VALUES
    ( 1, 'Wang', 'Allen' );
INSERT INTO Address ( AddressId, PersonId, City, State )
VALUES
    ( 1, 2, 'New York City', 'New York' );

解答

LEFT JOIN 返回左表中所有的行,即使右表中没有匹配

  • 如果右表没有匹配,则左表中的结果为NULL
  • 数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。

使用 on 和 where 条件的区别是:

  • on 条件是在生成临时表时用的条件
  • where 条件是在临时表生成后,再对临时表进行过滤的条件
# Write your MySQL query statement below
select
    p.FirstName, p.LastName, A.City, A.State
from
    Person as p left join Address as A
on
    p.PersonId = A.PersonId;

02 第二高的薪水

LeetCode176

题目描述

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

解答

方法1:

  • 先通过子查询找出最高薪水,得到一个max值
  • 把这个值作为一个条件,在主查询中找出比max值小的所有值,然后在这里面找出最大的一个就是第二高的薪水
# Write your MySQL query statement below
select
    max(Salary) as SecondHighestSalary
from
    Employee
where
    Salary < (select max(Salary) from Employee);

方法2:

  • 将薪水按薪水降序排列,并使用 distinct 关键字去重,然后使用关键字 limit 找出对应的记录即可
  • 数据库中,起始行(第一行)是0,所以第二行是1
# Write your MySQL query statement below
select(
    select distinct
        Salary 
    from
        Employee
    order by
        Salary
    desc
    limit
        1,1
) as SecondHighestSalary;

03 超过经理收入的员工

LeetCode181

题目描述

Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。

+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | NULL      |
| 4  | Max   | 90000  | NULL      |
+----+-------+--------+-----------+

给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

+----------+
| Employee |
+----------+
| Joe      |
+----------+

解答

方法1:

  • 对同一个表进行连接,使用 where 子句过滤,条件是 p1.ManagerId = p2.Id and p1.Salary > p2.Salary
# Write your MySQL query statement below
select
    p1.Name as Employee
from
    Employee as p1, Employee as p2
where
    p1.ManagerId = p2.Id and p1.Salary > p2.Salary;

方法2:

  • 使用 JOIN ON 来进行连接

04 查找重复的电子邮箱

LeetCode182

题目描述

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

示例:

+----+---------+
| Id | Email   |
+----+---------+
| 1  | a@b.com |
| 2  | c@d.com |
| 3  | a@b.com |
+----+---------+

根据以上输入,你的查询应返回以下结果:

+---------+
| Email   |
+---------+
| a@b.com |
+---------+

**说明:**所有电子邮箱都是小写字母。

解答

方法1:

  • 首先执行 from
  • 然后根据 Email 进行分组,在分组时利用 having 指定分组的过滤条件,找出行数大于1的所有分组
  • 对这些分组进行 select 查询,用关键字 distinct 去重
# Write your MySQL query statement below
select distinct
    Email
from
    Person
group by
    Email
having
    count(Email) > 1;

方法2:

  • 重复的邮箱里,名字相等,但 ID 不相等
# Write your MySQL query statement below
select distinct
    t1.Email
from
    Person as t1, Person as t2
where
    t1.Email = t2.Email and t1.Id != t2.Id;

05 从不订购的客户

LeetCode183

题目描述

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers 表:

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+

Orders 表:

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

例如给定上述表格,你的查询应返回:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

解答

方法1:

  • 连接两个表,左边表是Customers 表,右边表是 Orders
  • 由于在 Orders 表中不存在 ID 为2和4的顾客消费记录,没有匹配上他们的信息
  • 连接后的表如图
+----+-------+------------+
| Id | Name  | CustomerId |
+----+-------+------------+
| 1  | Joe   |      1     |
| 2  | Henry |    NULL    |
| 3  | Sam   |      3     |
| 4  | Max   |    NULL    |
+----+-------+------------+
# Write your MySQL query statement below
select
    c.Name as Customers
from
    Customers as c left join Orders as o
on
    c.Id = o.CustomerId
where
    o.Id is null;

方法2:

  • 先用子查询找出 Orders 表中的 CustomerId
  • 子查询的结果用于主查询中,找出Customers 表中 ID 不在子查询结果中的记录
# Write your MySQL query statement below
select
    Name as Customers
from
    Customers
where
    Id not in (select CustomerId from Orders);

06 删除重复的电子邮箱

LeetCode196

题目描述

编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小的那个

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+
Id 是这个表的主键。

例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+

解答

方法1:

  • 用连接,注意这里用>号,因为要保留ID最小的
# Write your MySQL query statement below
delete 
    t1
from
    Person as t1, Person as t2
where
    t1.Email = t2.Email and t1.Id > t2.Id;

方法2:

  • MYSQL 不能先select一个表的记录,然后在按此条件进行更新和删除同一个表的记录删除数据。因此创建临时表进行过渡。
# Write your MySQL query statement below
delete from
    Person
where
    Id not in (select 
                    * 
               from 
                    (select 
                        min(Id) as id 
                     from 
                        Person 
                     group by 
                        Email
                    ) as temp);

07 上升的温度

LeetCode197

题目描述

给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。

+---------+------------------+------------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
+---------+------------------+------------------+
|       1 |       2015-01-01 |               10 |
|       2 |       2015-01-02 |               25 |
|       3 |       2015-01-03 |               20 |
|       4 |       2015-01-04 |               30 |
+---------+------------------+------------------+

例如,根据上述给定的 Weather 表格,返回如下 Id:

+----+
| Id |
+----+
|  2 |
|  4 |
+----+

解答

方法1:

  • 使用 datediff 函数,求日期之差看是否为1。另一个条件是今天的温度比昨天的高
# Write your MySQL query statement below
select
    w1.Id
from
    Weather as w1, Weather as w2
where
    datediff(w1.RecordDate, w2.RecordDate) = 1 and w1.Temperature > w2.Temperature;

方法2:

  • 使用 subdate(a.RecordDate,1) = b.RecordDate)

08 大的国家

LeetCode595

题目描述

这里有张 World

+-----------------+------------+------------+--------------+---------------+
| name            | continent  | area       | population   | gdp           |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan     | Asia       | 652230     | 25500100     | 20343000      |
| Albania         | Europe     | 28748      | 2831741      | 12960000      |
| Algeria         | Africa     | 2381741    | 37100000     | 188681000     |
| Andorra         | Europe     | 468        | 78115        | 3712000       |
| Angola          | Africa     | 1246700    | 20609294     | 100990000     |
+-----------------+------------+------------+--------------+---------------+

如果一个国家的面积超过300万平方公里,或者人口超过2500万,那么这个国家就是大国家。

编写一个SQL查询,输出表中所有大国家的名称、人口和面积。

例如,根据上表,我们应该输出:

+--------------+-------------+--------------+
| name         | population  | area         |
+--------------+-------------+--------------+
| Afghanistan  | 25500100    | 652230       |
| Algeria      | 37100000    | 2381741      |
+--------------+-------------+--------------+

解答

# Write your MySQL query statement below
select 
    name, population, area
from
    World
where
    area > 3000000 or population > 25000000;

09 超过5名学生的课

LeetCode596

题目描述

有一个courses 表 ,有: **student (学生) **和 class (课程)

请列出所有超过或等于5名学生的课。

例如,表:

+---------+------------+
| student | class      |
+---------+------------+
| A       | Math       |
| B       | English    |
| C       | Math       |
| D       | Biology    |
| E       | Math       |
| F       | Computer   |
| G       | Math       |
| H       | Math       |
| I       | Math       |
+---------+------------+

应该输出:

+---------+
| class   |
+---------+
| Math    |
+---------+

Note:
学生在每个课中不应被重复计算。

解答

  • 对课程进行分组
  • 使用 distinct 避免同一个学生选了两次课
# Write your MySQL query statement below
select distinct
    class
from
    courses
where
    class in (select class from courses group by class having count(distinct student) >= 5);

或者

# Write your MySQL query statement below
select distinct
    class
from
    courses
group by
    class
having
    count(distinct student) >= 5;

10 有趣的电影

LeetCode620

题目描述

某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 LED显示板做电影推荐,上面公布着影评和相关电影描述。

作为该电影院的信息部主管,您需要编写一个 SQL查询,找出所有影片描述为 boring (不无聊) 的并且** id 为奇数 **的影片,结果请按等级 rating 排列。

例如,下表 cinema:

+---------+-----------+--------------+-----------+
|   id    | movie     |  description |  rating   |
+---------+-----------+--------------+-----------+
|   1     | War       |   great 3D   |   8.9     |
|   2     | Science   |   fiction    |   8.5     |
|   3     | irish     |   boring     |   6.2     |
|   4     | Ice song  |   Fantacy    |   8.6     |
|   5     | House card|   Interesting|   9.1     |
+---------+-----------+--------------+-----------+

对于上面的例子,则正确的输出是为:

+---------+-----------+--------------+-----------+
|   id    | movie     |  description |  rating   |
+---------+-----------+--------------+-----------+
|   5     | House card|   Interesting|   9.1     |
|   1     | War       |   great 3D   |   8.9     |
+---------+-----------+--------------+-----------+

解答

  • 根据条件进行过滤
  • 降序输出即可
# Write your MySQL query statement below
select
    id, movie, description, rating
from
    cinema
where
    description != 'boring' and id % 2 = 1
order by
    rating
desc;

11 交换工资

LeetCode627

题目描述

给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求只使用一个更新(Update)语句,并且没有中间的临时表。

注意,您必只能写一个 Update 语句,请不要编写任何 Select 语句。

例如:

| id | name | sex | salary |
|----|------|-----|--------|
| 1  | A    | m   | 2500   |
| 2  | B    | f   | 1500   |
| 3  | C    | m   | 5500   |
| 4  | D    | f   | 500    |

运行你所编写的更新语句之后,将会得到以下表:

| id | name | sex | salary |
|----|------|-----|--------|
| 1  | A    | f   | 2500   |
| 2  | B    | m   | 1500   |
| 3  | C    | f   | 5500   |
| 4  | D    | m   | 500    |

解答

方法1:

  • 使用 IF 语句

格式为 IF(表达式1,表达式2,表达式3)

  • 如果表达式1为真,就返回表达式2,否则返回表达式3
# Write your MySQL query statement below
update
    salary
set
    sex = if(sex = 'm', 'f', 'm');

方法2:

  • 使用 case 语句

格式为 case 列名 when 条件1 then 表达式1 … else 表达式n end

# Write your MySQL query statement below
update
    salary
set
    sex = (case sex when 'm' then 'f'
                    when 'f' then 'm'
          end);

二、中等

01 第 N 高的薪水

LeetCode177

题目描述

编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null

+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+

解答

  • 去重,降序,输出第 N 高的薪水即可
  • 注意 limit 处的 N 不能是一个表达式,如N - 1
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    set N = N - 1;
  RETURN (
      # Write your MySQL query statement below.
      select distinct
        Salary 
      from
        Employee
      order by
        Salary
      desc
      limit N, 1
  );
END

02 分数排名

LeetCode178

题目描述

编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。

+----+-------+
| Id | Score |
+----+-------+
| 1  | 3.50  |
| 2  | 3.65  |
| 3  | 4.00  |
| 4  | 3.85  |
| 5  | 4.00  |
| 6  | 3.65  |
+----+-------+

例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):

+-------+------+
| Score | Rank |
+-------+------+
| 4.00  | 1    |
| 4.00  | 1    |
| 3.85  | 2    |
| 3.65  | 3    |
| 3.65  | 3    |
| 3.50  | 4    |
+-------+------+

解答

方法1:

  • 第一列降序排列
  • 第二列,找出大于等于此分数的值的不重复的个数
# Write your MySQL query statement below
select
    Score, (select 
                count(distinct Score) 
            from 
                Scores
            where
                Score >= s.Score) as Rank
from
    Scores as s
order by
    Score
desc;     

方法2:

# Write your MySQL query statement below
SELECT
    S1.score,
    COUNT( DISTINCT S2.score ) Rank
FROM
    Scores as S1
    INNER JOIN Scores as S2
    ON S1.score <= S2.score
GROUP BY
    S1.id, S1.score
ORDER BY
    S1.score DESC;

03 连续出现的数字

LeetCode180

题目描述

编写一个 SQL 查询,查找所有至少连续出现三次的数字。

+----+-----+
| Id | Num |
+----+-----+
| 1  |  1  |
| 2  |  1  |
| 3  |  1  |
| 4  |  2  |
| 5  |  1  |
| 6  |  2  |
| 7  |  2  |
+----+-----+

例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

+-----------------+
| ConsecutiveNums |
+-----------------+
| 1               |
+-----------------+

解答

方法1:

  • 连续出现3次,则它们的 ID 之差是 1,Num 的值相等
# Write your MySQL query statement below
select distinct
    t1.Num as ConsecutiveNums
from 
    Logs as t1, Logs as t2, Logs as t3
where
    t1.Id = t2.Id - 1 and
    t2.Id = t3.Id - 1 and
    t1.Num = t2.Num and
    t2.Num = t3.Num;

方法2:

# Write your MySQL query statement below
select distinct
    t1.Num as ConsecutiveNums
from 
    Logs as t1 join Logs as t2 
on
    t2.Id - 1 = t1.Id and t2.Num = t1.Num join Logs as t3
on
    t3.Id - 1 = t2.Id and t3.Num = t2.Num;

04 部门工资最高的员工

LeetCode184

题目描述

Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
+----+-------+--------+--------------+

Department 表包含公司所有部门的信息。

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+

解答

方法1:

# Write your MySQL query statement below
select
    D.Name as Department, 
    E.Name as Employee, 
    E.Salary
from
    Employee as E, Department as D
where
    E.DepartmentId = D.Id and 
    (E.Salary, E.DepartmentId) in (select max(Salary), DepartmentId from Employee group by DepartmentId);

方法2:

# Write your MySQL query statement below
select
    D.Name as Department, 
    E.Name as Employee, 
    E.Salary
from
    Employee as E , Department as D
where
    DepartmentId = D.Id and 
    Salary = (select max(Salary) from Employee where DepartmentId = D.Id);    

05 换座位

LeetCode626

小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。

其中纵列的 **id **是连续递增的

小美想改变相邻俩学生的座位。

你能不能帮她写一个 SQL query 来输出小美想要的结果呢?

示例:

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Abbot   |
|    2    | Doris   |
|    3    | Emerson |
|    4    | Green   |
|    5    | Jeames  |
+---------+---------+

假如数据输入的是上表,则输出结果如下:

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Doris   |
|    2    | Abbot   |
|    3    | Green   |
|    4    | Emerson |
|    5    | Jeames  |
+---------+---------+

注意:

如果学生人数是奇数,则不需要改变最后一个同学的座位。

解答

方法1:

  • 使用 case when...then...else...end来交换 ID,再按交换后的 ID 进行排序即可
# Write your MySQL query statement below
select
    (case
        when id mod 2 = 0 then id - 1
        when id mod 2 = 1 and 
             id != (select 
                        count(id)
                    from
                        seat) then id + 1
        else id
    end) as id, student
from
    seat
order by
    id;

方法2:

  • 使用 union 组合两个或更多 select 语句的结果
  • 这里没有重复值,所以不需要使用 union all
# Write your MySQL query statement below
select
    *
from (
    select id, student from seat where id mod 2 = 1 and id = (select count(id) from seat)
union
    select id - 1, student from seat where id mod 2 = 0
union
    select id + 1, student from seat where id mod 2 = 1 and id != (select count(id) from seat)
) as tmp
order by
    id;

三、困难

01 部门工资前三高的员工

LeetCode185

题目描述

Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id 。

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
| 5  | Janet | 69000  | 1            |
| 6  | Randy | 85000  | 1            |
+----+-------+--------+--------------+

Department 表包含公司所有部门的信息。

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

编写一个 SQL 查询,找出每个部门工资前三高的员工。例如,根据上述给定的表格,查询结果应返回:

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+

解答

  • 暂无

02 行程和用户

LeetCode262

Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。

+----+-----------+-----------+---------+--------------------+----------+
| Id | Client_Id | Driver_Id | City_Id |        Status      |Request_at|
+----+-----------+-----------+---------+--------------------+----------+
| 1  |     1     |    10     |    1    |     completed      |2013-10-01|
| 2  |     2     |    11     |    1    | cancelled_by_driver|2013-10-01|
| 3  |     3     |    12     |    6    |     completed      |2013-10-01|
| 4  |     4     |    13     |    6    | cancelled_by_client|2013-10-01|
| 5  |     1     |    10     |    1    |     completed      |2013-10-02|
| 6  |     2     |    11     |    6    |     completed      |2013-10-02|
| 7  |     3     |    12     |    6    |     completed      |2013-10-02|
| 8  |     2     |    12     |    12   |     completed      |2013-10-03|
| 9  |     3     |    10     |    12   |     completed      |2013-10-03| 
| 10 |     4     |    13     |    12   | cancelled_by_driver|2013-10-03|
+----+-----------+-----------+---------+--------------------+----------+

Users 表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。

+----------+--------+--------+
| Users_Id | Banned |  Role  |
+----------+--------+--------+
|    1     |   No   | client |
|    2     |   Yes  | client |
|    3     |   No   | client |
|    4     |   No   | client |
|    10    |   No   | driver |
|    11    |   No   | driver |
|    12    |   No   | driver |
|    13    |   No   | driver |
+----------+--------+--------+

写一段 SQL 语句查出 **2013年10月1日 **至 **2013年10月3日 **期间非禁止用户的取消率。基于上表,你的 SQL 语句应返回如下结果,取消率(Cancellation Rate)保留两位小数。

+------------+-------------------+
|     Day    | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 |       0.33        |
| 2013-10-02 |       0.00        |
| 2013-10-03 |       0.50        |
+------------+-------------------+

解答

  • 暂无

03 体育馆的人流量

LeetCode601

题目描述

X 市建了一个新的体育馆,每日人流量信息被记录在这三列信息中:序号 (id)、日期 (date)、 人流量(people)。

请编写一个查询语句,找出高峰期时段,要求连续三天及以上,并且每天人流量均不少于100。

例如,表 stadium

+------+------------+-----------+
| id   | 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-08 | 188       |
+------+------------+-----------+

对于上面的示例数据,输出为:

+------+------------+-----------+
| id   | date       | people    |
+------+------------+-----------+
| 5    | 2017-01-05 | 145       |
| 6    | 2017-01-06 | 1455      |
| 7    | 2017-01-07 | 199       |
| 8    | 2017-01-08 | 188       |
+------+------------+-----------+

Note:
每天只有一行记录,日期随着 id 的增加而增加。

解答

  • 暂无
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值