使用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
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM