MySQL数据库建立日历可以通过以下步骤进行:
CREATE TABLE calendar ( date DATE PRIMARY KEY, day_of_week INT, year INT, month INT, day INT, quarter INT );
INSERT INTO calendar (date, day_of_week, year, month, day, quarter) SELECT date, DAYOFWEEK(date) AS day_of_week, YEAR(date) AS year, MONTH(date) AS month, DAY(date) AS day, QUARTER(date) AS quarter FROM ( SELECT DATE('2010-01-01') + INTERVAL n DAY AS date FROM ( SELECT a.N + b.N * 10 + c.N * 100 AS n FROM (SELECT 0 AS N 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) a, (SELECT 0 AS N 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) b, (SELECT 0 AS N 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) c ) numbers WHERE DATE('2010-01-01') + INTERVAL n DAY <= '2025-12-31' ) dates;
SELECT * FROM calendar WHERE year = 2022 AND month = 5;
优势:
应用场景:
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云