在SQL中,可以使用日期函数和条件语句来获取前3个月的月份名称和每个月的最后日期。
首先,我们可以使用DATE_SUB函数来获取当前日期的前3个月的日期。然后,使用MONTH函数和CASE语句来获取月份名称。最后,使用LAST_DAY函数来获取每个月的最后日期。
以下是一个示例查询:
SELECT
CASE
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 1 THEN 'January'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 2 THEN 'February'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 3 THEN 'March'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 4 THEN 'April'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 5 THEN 'May'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 6 THEN 'June'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 7 THEN 'July'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 8 THEN 'August'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 9 THEN 'September'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 10 THEN 'October'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 11 THEN 'November'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) = 12 THEN 'December'
END AS MonthName,
LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) AS LastDateOfMonth
UNION ALL
SELECT
CASE
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 1 THEN 'January'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 2 THEN 'February'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 3 THEN 'March'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 4 THEN 'April'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 5 THEN 'May'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 6 THEN 'June'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 7 THEN 'July'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 8 THEN 'August'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 9 THEN 'September'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 10 THEN 'October'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 11 THEN 'November'
WHEN MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) = 12 THEN 'December'
END AS MonthName,
LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) AS LastDateOfMonth
UNION ALL
SELECT
CASE
WHEN MONTH(CURDATE()) = 1 THEN 'January'
WHEN MONTH(CURDATE()) = 2 THEN 'February'
WHEN MONTH(CURDATE()) = 3 THEN 'March'
WHEN MONTH(CURDATE()) = 4 THEN 'April'
WHEN MONTH(CURDATE()) = 5 THEN 'May'
WHEN MONTH(CURDATE()) = 6 THEN 'June'
WHEN MONTH(CURDATE()) = 7 THEN 'July'
WHEN MONTH(CURDATE()) = 8 THEN 'August'
WHEN MONTH(CURDATE()) = 9 THEN 'September'
WHEN MONTH(CURDATE()) = 10 THEN 'October'
WHEN MONTH(CURDATE()) = 11 THEN 'November'
WHEN MONTH(CURDATE()) = 12 THEN 'December'
END AS MonthName,
LAST_DAY(CURDATE()) AS LastDateOfMonth;
这个查询将返回前3个月的月份名称和每个月的最后日期。你可以根据需要进行调整和优化。
关于腾讯云相关产品和产品介绍链接地址,由于不能提及具体品牌商,建议你访问腾讯云官方网站,查找与云计算相关的产品和服务。在腾讯云的官方网站上,你可以找到各种云计算解决方案、云服务器、云数据库、云存储等产品,以满足你的需求。
领取专属 10元无门槛券
手把手带您无忧上云