使用SQL获取两个日期范围之间的所有日期列表
1、sql查询中,常常涉及到获取两个日期之间的所有日期列表,可将如下sql创建成视图,然后使用between and即可完成该需求。
select dateStr, CASE DAYOFWEEK(dateStr) WHEN '1' THEN '星期日' WHEN '2' THEN '星期一' WHEN '3' THEN '星期二' WHEN '4' THEN '星期三' WHEN '5' THEN '星期四' WHEN '6' THEN '星期五' ELSE '星期六' END week from ( select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) dateStr from (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4 ) v