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

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

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语句如下:

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

在这里插入图片描述

posted @ 2023-01-11 21:54  福大大架构师每日一题  阅读(27)  评论(0编辑  收藏  举报  来源