首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >2023-01-11:体育馆的人流量。编写一个 SQL 查询以找出每行的人数大于或等于 100 且 id 连续的三行或更多行记录。返回按 visit_date

2023-01-11:体育馆的人流量。编写一个 SQL 查询以找出每行的人数大于或等于 100 且 id 连续的三行或更多行记录。返回按 visit_date

原创
作者头像
福大大架构师每日一题
发布于 2023-01-11 22:10:06
发布于 2023-01-11 22:10:06
1.1K0
举报

2023-01-11:体育馆的人流量。编写一个 SQL 查询以找出每行的人数大于或等于 100 且 id 连续的三行或更多行记录。返回按 visit_date 升序排列 的结果表。

代码语言:sql
复制
DROP TABLE IF EXISTS `stadium`;
CREATE TABLE `stadium` (
  `id` int(11) NOT NULL,
  `visit_date` date NOT NULL,
  `people` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `stadium` VALUES ('1', '2017-01-01', '10');
INSERT INTO `stadium` VALUES ('2', '2017-01-02', '109');
INSERT INTO `stadium` VALUES ('3', '2017-01-03', '150');
INSERT INTO `stadium` VALUES ('4', '2017-01-04', '99');
INSERT INTO `stadium` VALUES ('5', '2017-01-05', '145');
INSERT INTO `stadium` VALUES ('6', '2017-01-06', '1455');
INSERT INTO `stadium` VALUES ('7', '2017-01-07', '199');
INSERT INTO `stadium` VALUES ('8', '2017-01-09', '188');

答案2023-01-11:

sql语句如下:

代码语言:sql
复制
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
在这里插入图片描述
在这里插入图片描述

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档