,可以通过以下步骤实现:
dates
的日期表:CREATE TABLE dates (
date_value DATE PRIMARY KEY
);
dates
表中:INSERT INTO dates (date_value)
SELECT CURDATE() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY
FROM (SELECT 0 AS a 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 a
CROSS JOIN (SELECT 0 AS a 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 b
CROSS JOIN (SELECT 0 AS a 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 c
这将插入从过去到当前日期的所有日期值。
SELECT MAX(date_value) AS latest_date
FROM dates
WHERE date_value <= CURDATE();
这将返回最近的日期。
通过以上步骤,我们成功地在MySQL中加入了过去最近的日期表。
云+社区沙龙online [技术应变力]
云+社区沙龙online[数据工匠]
企业创新在线学堂
云+社区沙龙online第5期[架构演进]
云+社区技术沙龙[第17期]
企业创新在线学堂
云+社区技术沙龙[第7期]
TC-Day
TC-Day
云+社区沙龙online第6期[开源之道]
领取专属 10元无门槛券
手把手带您无忧上云