gpt4 book ai didi

mysql - 派生表查询在mysql中不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:37 25 4
gpt4 key购买 nike

以下查询导致错误。

select monstart,
sum(datediff(least(m.monend, t.end_date) + interval 1 day,
greatest(m.monstart, t.start_date)
)
) as days_worked
from travel t join
(select date('2016-01-01') as monstart, date('2016-01-31') as monend union all
select date('2016-02-01') as monstart, date('2016-02-29') as monend union all
select date('2016-03-01') as monstart, date('2016-03-31') as monend union all
select date('2016-04-01') as monstart, date('2016-04-30') as monend union all
select date('2016-05-01') as monstart, date('2016-05-31') as monend union all
select date('2016-06-01') as monstart, date('2016-06-30') as monend union all
select date('2016-07-01') as monstart, date('2016-07-31') as monend union all
select date('2016-08-01') as monstart, date('2016-08-31') as monend union all
select date('2016-09-01') as monstart, date('2016-09-30') as monend union all
select date('2016-10-01') as monstart, date('2016-10-31') as monend union all
select date('2016-11-01') as monstart, date('2016-11-30') as monend union all
select date('2016-12-01') as monstart, date('2016-12-31') as monend union all
) m
on t.end_date >= m.monstart and t.start_date <= m.monend
group by m.monstart;

错误如下

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') m
on t.end_date >= m.monstart and t.start_date <= m.monend
group by m.mon' at line 19

最佳答案

最后你有一个额外的union all。只需将其删除即可。

select monstart,
sum(datediff(least(m.monend, t.end_date) + interval 1 day,
greatest(m.monstart, t.start_date)
)
) as days_worked
from travel t join
(select date('2016-01-01') as monstart, date('2016-01-31') as monend union all
select date('2016-02-01') as monstart, date('2016-02-29') as monend union all
select date('2016-03-01') as monstart, date('2016-03-31') as monend union all
select date('2016-04-01') as monstart, date('2016-04-30') as monend union all
select date('2016-05-01') as monstart, date('2016-05-31') as monend union all
select date('2016-06-01') as monstart, date('2016-06-30') as monend union all
select date('2016-07-01') as monstart, date('2016-07-31') as monend union all
select date('2016-08-01') as monstart, date('2016-08-31') as monend union all
select date('2016-09-01') as monstart, date('2016-09-30') as monend union all
select date('2016-10-01') as monstart, date('2016-10-31') as monend union all
select date('2016-11-01') as monstart, date('2016-11-30') as monend union all
select date('2016-12-01') as monstart, date('2016-12-31') as monend -- removed a union all from here
) m
on t.end_date >= m.monstart and t.start_date <= m.monend
group by m.monstart;

关于mysql - 派生表查询在mysql中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42184880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com