• 601-体育馆的人流量


    601-体育馆的人流量

    1. 题目

    在这里插入图片描述
    在这里插入图片描述

    2. 思路

    思路:查询Stadium表中人流量超过100的记录,将查询结果与自身的临时表连接,再使用where获得满足条件的记录

    1. 查询Stadium表中人流量超过100的记录

      select t1.*
      from Stadium t1, Stadium t2, Stadium t3
      where t1.people >= 100 and t2.people >= 100 and t3.people >= 100;
      
      • 1
      • 2
      • 3

      查询结果如下:

      idvisit_datepeople
      22017-01-02109
      32017-01-03150
      52017-01-05145
      62017-01-061455
      72017-01-07199
      82017-01-09188
      22017-01-02109
      32017-01-03150
      52017-01-05145
      62017-01-061455
      72017-01-07199
      82017-01-09188
      22017-01-02109
      32017-01-03150
      52017-01-05145
      62017-01-061455
      72017-01-07199
      82017-01-09188
      22017-01-02109
      32017-01-03150
      52017-01-05145
      62017-01-061455
      72017-01-07199
      82017-01-09188
      22017-01-02109
      32017-01-03150
      52017-01-05145
      62017-01-061455
      72017-01-07199
      82017-01-09188
      22017-01-02109
      32017-01-03150
      52017-01-05145
      62017-01-06
    2. 添加id连续的三行或更多记录

      select t1.*
      from Stadium t1, Stadium t2, Stadium t3
      where t1.people >= 100 and t2.people >= 100 and t3.people >= 100
      # 添加id连续的三行或更多记录
      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 - t1.id = 2 and t3.id - t2.id = 1 and t2.id - t1.id = 1)  # t3 t2 t1
      );
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      idvisit_datepeople
      62017-01-061455
      72017-01-07199
      72017-01-07199
      82017-01-09188
      52017-01-05145
      62017-01-061455
    3. distinct去重
      在这里插入图片描述

      idvisit_datepeople
      62017-01-061455
      72017-01-07199
      82017-01-09188
      52017-01-05145
    4. 根据id排序

      在这里插入图片描述
      在这里插入图片描述

    3. 解决

    # Write your MySQL query statement below
    # 思路:查询Stadium表中人流量超过100的记录,将查询结果与自身的临时表连接,再使用where获得满足条件的记录
    
    # 第一步:查询人流量超过100的记录
    select distinct t1.*
    from Stadium t1, Stadium t2, Stadium t3
    where t1.people >= 100 and t2.people >= 100 and t3.people >= 100
    # 添加id连续的三行或更多记录
    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 - t1.id = 2 and t3.id - t2.id = 1 and t2.id - t1.id = 1)  # t3 t2 t1
    )
    order by t1.id;
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    4. 运行结果

    在这里插入图片描述

  • 相关阅读:
    MAC下IDEA快捷键
    css定位与布局 2
    王怀远:阿里云一站式物联网存储架构设计
    初级前端开发岗
    【无标题】
    Linux驱动实现IO模型
    微信公众号第三方平台开发,零基础入门。想学我教你啊
    有关嵌入式的 github
    手写数字识别-基于卷积神经网络
    「Git|场景案例」从项目中删除之前commit过的文件并且让git不追踪删除操作
  • 原文地址:https://blog.csdn.net/weixin_46592197/article/details/133379014