MySql查询12个月份

一、月份(1,2,3,4,5,6,7,8,9,10,11,12)

SELECT
	DATE_FORMAT( CURDATE( ), '%c' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 1 MONTH, '%c' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 2 MONTH, '%c' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 3 MONTH, '%c' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 4 MONTH, '%c' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 5 MONTH, '%c' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 6 MONTH, '%c' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 7 MONTH, '%c' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 8 MONTH, '%c' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 9 MONTH, '%c' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 10 MONTH, '%c' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 11 MONTH, '%c' ) AS MONTH

更简洁的写法:

SELECT
	1 AS MONTH UNION
SELECT
	2 AS MONTH UNION
SELECT
	3 AS MONTH UNION
SELECT
	4 AS MONTH UNION
SELECT
	5 AS MONTH UNION
SELECT
	6 AS MONTH UNION
SELECT
	7 AS MONTH UNION
SELECT
	8 AS MONTH UNION
SELECT
	9 AS MONTH UNION
SELECT
	10 AS MONTH UNION
SELECT
	11 AS MONTH UNION
SELECT
	12 AS MONTH

运行结果:

 二、月份(01,02,03,04,05,06,07,08,09,10,11,12)

SELECT
	DATE_FORMAT( CURDATE( ), '%m' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 1 MONTH, '%m' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 2 MONTH, '%m' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 3 MONTH, '%m' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 4 MONTH, '%m' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 5 MONTH, '%m' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 6 MONTH, '%m' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 7 MONTH, '%m' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 8 MONTH, '%m' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 9 MONTH, '%m' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 10 MONTH, '%m' ) AS MONTH UNION
SELECT
	DATE_FORMAT( CURDATE( ) - INTERVAL 11 MONTH, '%m' ) AS MONTH

更简洁的写法:

SELECT
	01 AS MONTH UNION
SELECT
	02 AS MONTH UNION
SELECT
	03 AS MONTH UNION
SELECT
	04 AS MONTH UNION
SELECT
	05 AS MONTH UNION
SELECT
	06 AS MONTH UNION
SELECT
	07 AS MONTH UNION
SELECT
	08 AS MONTH UNION
SELECT
	09 AS MONTH UNION
SELECT
	10 AS MONTH UNION
SELECT
	11 AS MONTH UNION
SELECT
	12 AS MONTH

运行结果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值