我希望你能弄清楚其餘的。
select * from (
select date_add('2003-01-01 00:00:00.000', INTERVAL n5.num*10000+n4.num*1000+n3.num*100+n2.num*10+n1.num DAY) as date from
(select 0 as num
union all select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9) n1,
(select 0 as num
union all select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9) n2,
(select 0 as num
union all select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9) n3,
(select 0 as num
union all select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9) n4,
(select 0 as num
union all select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9) n5
) a
where date >'2011-01-02 00:00:00.000' and date < NOW()
order by date
隨着
select n3.num*100+n2.num*10+n1.num as date
你會得到與數字的列從0到最大(N3)* 100 + MAX(N2)* 10 + MAX(N1)
因爲我們在這裏將最大n3設置爲3,SELECT將返回399,再加上0 - > 400條記錄(日曆中的日期)。
您可以通過限制它來調整動態日曆,例如,從min(日期)到現在()。
我認爲這是一個絕妙的訣竅。得到我的投票。我猜這個查詢可以用來填充「日曆」表。 – 2013-07-05 21:23:46
這是我見過的最乾淨的解決方案。真棒 – 2016-06-08 11:10:08