LeetCode--601.体育馆的人流量

    科技2022-07-10  163

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

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

    Create table If Not Exists stadium (id int,date DATE NULL, people int); Truncate table stadium; insert into stadium (id, date, people) values('1', '2017-01-01', '10'); insert into stadium (id, date, people) values('2', '2017-01-02', '109'); insert into stadium (id, date, people) values('3', '2017-01-03', '150'); insert into stadium (id, date, people) values('4', '2017-01-04', '99'); insert into stadium (id, date, people) values('5', '2017-01-05', '145'); insert into stadium (id, date, people) values('6', '2017-01-06', '1455'); insert into stadium (id, date, people) values('7', '2017-01-07', '199'); insert into stadium (id, date, people) values('8', '2017-01-08', '188');

    拆分看题,三表自联查,每个的人数都大于100,

    (1)s1.id-s2.id=1,s2.id-s3.id=1,相当于s3 s2 s1 的顺序三个连续的

    (2)s2.id-s1.id=1,s1.id-s3.id=1,相当于s3 s1 s2 的顺序三个连续的

    (3)s3.id-s2.id=1,s2.id-s1.id=1,相当于s1 s2 s3 的顺序三个连续的

    select distinct s1.* from stadium s1, stadium s2, stadium s3 where s1.people >= 100 and s2.people >= 100 and s3.people >= 100 and ( (s1.id-s2.id=1 and s2.id-s3.id=1) or (s2.id-s1.id=1 and s1.id-s3.id=1) or (s3.id-s2.id=1 and s2.id-s1.id=1) ) ORDER BY s1.id
    Processed: 0.037, SQL: 8