Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 根据剧院的日期间隔生成结果_Mysql_Sql_Join_Window Functions_Gaps And Islands - Fatal编程技术网

Mysql 根据剧院的日期间隔生成结果

Mysql 根据剧院的日期间隔生成结果,mysql,sql,join,window-functions,gaps-and-islands,Mysql,Sql,Join,Window Functions,Gaps And Islands,我正在处理一个SQL查询,其中我有两个名为Theatre的表,电影如下: 剧院 电影 剧院定期放映相同的电影,直到该剧院的放映日期相差超过2天 对于剧院,如果放映日期差异超过2天,则会更改为电影列表表中的下一部电影 所有电影院都按相同的顺序放映电影 现在我想为这个场景编写一个查询 预期的结果是:我为每一行添加了注释作为解释 脚本: 有点复杂,;这里是一个使用窗口函数的方法,在MySQL 8.0中提供 我理解你的问题,你需要把剧院里的一排排排成一组,然后再分配给同一部电影。为此,您可以使用lag检

我正在处理一个SQL查询,其中我有两个名为Theatre的表,电影如下:

剧院

电影

剧院定期放映相同的电影,直到该剧院的放映日期相差超过2天

对于剧院,如果放映日期差异超过2天,则会更改为电影列表表中的下一部电影

所有电影院都按相同的顺序放映电影

现在我想为这个场景编写一个查询

预期的结果是:我为每一行添加了注释作为解释

脚本:


有点复杂,;这里是一个使用窗口函数的方法,在MySQL 8.0中提供

我理解你的问题,你需要把剧院里的一排排排成一组,然后再分配给同一部电影。为此,您可以使用lag检索上一个show_日期,并在每次间隔超过2天时进行窗口总和递增。然后,您可以打开电影表并为每个组分配一部电影:

select t.id, t.show_date, m.name movie
from (
    select 
        t.*, 
        sum(case when show_date <= lag_show_date + interval 2 day then 0 else 1 end) 
            over(partition by id order by show_date) grp
    from (
        select 
            t.*, 
            lag(show_date) over(partition by id order by show_date) lag_show_date
        from theatre t
    ) t
) t
inner join (
    select m.*, row_number() over(order by id) grp from movie m
) m
    on m.grp = t.grp

您能帮我一下吗,使用这个查询,我得到了类似于第29行的错误1064 42000的错误:您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在第行选择“按id顺序分区按显示日期grp”6@learner:您正在运行哪个版本的MySQL?选择使用联机工具的版本https://paiza.io/en/languages/mysql,这里的版本是5.7.28-0ubuntu0.18.04.4,是否有更好的在线工具或mysql支持此功能query@learner. 这个查询使用窗口函数,它需要MySQL 8.0,正如我在回答的开头所解释的。所以它不会在你的版本上运行。如果我正确理解了您的任务,那么如果您不能使用窗口函数,就很难解决这个问题……请注意,按照惯例,一个名为id的列通常被假定为代理主键。
Movie with columns movie id and movie name:

id  name
----------
1   Avatar
2   Spiderman
3   Avengers
theatre_id  show_date   movie
---------------------------------------------

1           2018-05-01  Avatar  /* 1st theatre, 1st date occurrence so picking 1st movie
2           2018-05-01  Avatar  /* 2nd theatre, 1st date occurrence so picking 1st movie */
1           2018-05-03  Avatar  /* 1st theatre, with 1 day gap (1st may 2018 to 3rd may 2018), so picking 1st movie
3           2018-05-04  Avatar  /* 3rd theatre, 1st date occurrence so picking 1st movie */
2           2018-05-10  Spiderman /* 2nd theatre, with 8 days gap (1st may 2018 to 10th may 2018), so picking 2nd movie */
3           2018-05-11  Spiderman /* 3rd theatre, with 6 days gap (4th may 2018 to 11th may 2018) so picking 2nd movie */
2           2018-05-14  Avengers /* 2nd theatre, with 3 days gap (10th may 2018 to 14th may 2018), so picking 3rd movie */
drop table if exists theatre;

CREATE TABLE theatre ( 
  id INTEGER NOT NULL,
  show_date date NOT NULL
);

drop table if exists movie;

CREATE TABLE movie ( 
  id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(60) NOT NULL
);


insert into movie values (null, 'Avatar');
insert into movie values (null, 'Spiderman');
insert into movie values (null, 'Avengers');

insert into theatre values( 1, cast('2018-05-01' as date));
insert into theatre values( 2, cast('2018-05-01' as date));
insert into theatre values( 1, cast('2018-05-03' as date));
insert into theatre values( 3, cast('2018-05-04' as date));
insert into theatre values( 2, cast('2018-05-10' as date));
insert into theatre values( 3, cast('2018-05-11' as date));
insert into theatre values( 2, cast('2018-05-14' as date));
select t.id, t.show_date, m.name movie
from (
    select 
        t.*, 
        sum(case when show_date <= lag_show_date + interval 2 day then 0 else 1 end) 
            over(partition by id order by show_date) grp
    from (
        select 
            t.*, 
            lag(show_date) over(partition by id order by show_date) lag_show_date
        from theatre t
    ) t
) t
inner join (
    select m.*, row_number() over(order by id) grp from movie m
) m
    on m.grp = t.grp