以下查詢導致錯誤。派生表查詢不能在mysql中工作
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
它看起來像一個頗爲曲折的查詢BTW – Strawberry
你能解釋一下嗎?間接地, – DesirePRG
。見http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry