MySQL是一個(gè)強(qiáng)大的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),用于存儲(chǔ)和管理數(shù)據(jù)。在實(shí)際開發(fā)中,經(jīng)常需要對(duì)數(shù)據(jù)進(jìn)行統(tǒng)計(jì)和分析,經(jīng)常需要處理日期數(shù)據(jù)。本文將介紹如何使用MySQL生成本月每天的日期。
-- 生成本月的日期列表 SELECT DATE_FORMAT( DATE_ADD( CURDATE(), INTERVAL (0 - DAY(CURDATE()) + 1) DAY ), "%Y-%m-%d" ) AS start_date, DATE_FORMAT( DATE_ADD( CURDATE(), INTERVAL (0 - DAY(CURDATE()) + 1 + DAY(LAST_DAY(CURDATE()))) DAY ), "%Y-%m-%d" ) AS end_date INTO @start_date, @end_date; -- 生成日期列表 SELECT DATE( DATE_ADD(@start_date, INTERVAL t.n DAY) ) AS date FROM ( SELECT (SELECT 0 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) AS n FROM (SELECT 0) a CROSS JOIN (SELECT 0) b CROSS JOIN (SELECT 0) c ) t WHERE DATE_ADD(@start_date, INTERVAL t.n DAY)<= @end_date;
以上是生成本月每天日期的MySQL查詢語句,在MySQL中運(yùn)行該查詢語句,即可輸出本月每一天的日期。