我有以下存储过程从MySql中获取当前日期起的最近12个月
DELIMITER $$
CREATE
PROCEDURE `Calendar`()
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS `Cal` (
`month` INT(11) ,
`year` INT(11)
);
TRUNCATE TABLE Cal;
SET @counter := -1;
WHILE (DATEDIFF(DATE(NOW()), DATE(DATE_SUB(NOW(),INTERVAL @counter MONTH))) < DATEDIFF(DATE(NOW()), DATE(DATE_SUB(NOW(),INTERVAL 12 MONTH)))) DO
INSERT INTO Cal SELECT DATE_FORMAT(DATE_ADD(DATE_SUB(NOW(),INTERVAL 12 MONTH), INTERVAL @counter:=@counter + 1 MONTH),'%m'),DATE_FORMAT(DATE_ADD(DATE_SUB(NOW(),INTERVAL 12 MONTH), INTERVAL @counter + 1 MONTH),'%Y');
END WHILE;
SELECT * FROM `Cal`;
END$$
DELIMITER ;
DROP PROCEDURE IF EXISTS Calendar;
输出表是
9 2017
10 2017
11 2017
12 2018
1 2018
2 2018
3 2018
4 2018
5 2018
6 2018
7 2018
8 2018
9 2018
在第12个月中,年份是2018。应该是2017,并且行9 2017
不应自第13个月(包括当月)开始出现。我如何将第12个月定为2017年,并去除9 2017
预先致谢
答案 0 :(得分:0)
您也可以在不创建存储过程,临时表,将其插入临时表并将存储过程放在
之后的情况下进行操作
通过在MySQL代码中使用数字生成器。
查询
SELECT
MONTH(NOW() - INTERVAL number_generator.number MONTH) AS `month`
, YEAR(NOW() - INTERVAL number_generator.number MONTH) AS `year`
FROM (
SELECT
(@NUMBER := @NUMBER + 1) AS number
FROM (
SELECT 1 AS number UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
) AS records_1
CROSS JOIN (
SELECT 1 AS number UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
) AS records_2
CROSS JOIN (SELECT @NUMBER := 0) AS init_user_params
) AS number_generator
WHERE
number_generator.number BETWEEN 1 AND 12
AND
MONTH(NOW()) <> MONTH(NOW() - INTERVAL number_generator.number MONTH)
ORDER BY
YEAR(NOW() - INTERVAL number_generator.number MONTH) ASC
, MONTH(NOW() - INTERVAL number_generator.number MONTH) ASC
结果
| month | year |
|-------|------|
| 10 | 2017 |
| 11 | 2017 |
| 12 | 2017 |
| 1 | 2018 |
| 2 | 2018 |
| 3 | 2018 |
| 4 | 2018 |
| 5 | 2018 |
| 6 | 2018 |
| 7 | 2018 |
| 8 | 2018 |
参见演示http://sqlfiddle.com/#!9/340e01/584
请注意,如果您使用的是MySQL 8.0+,则使用通用表表达式会更清楚地了解语法。
查询
WITH RECURSIVE number_generator (number) AS
(
SELECT 1 AS number
UNION ALL
SELECT number + 1 FROM number_generator WHERE number < 12
)
SELECT
MONTH(NOW() - INTERVAL number_generator.number MONTH) AS `month`
, YEAR(NOW() - INTERVAL number_generator.number MONTH) AS `year`
FROM
number_generator
WHERE
MONTH(NOW()) <> MONTH(NOW() - INTERVAL number_generator.number MONTH)
ORDER BY
YEAR(NOW() - INTERVAL number_generator.number MONTH) ASC
, MONTH(NOW() - INTERVAL number_generator.number MONTH) ASC
结果
| month | year |
|-------|------|
| 10 | 2017 |
| 11 | 2017 |
| 12 | 2017 |
| 1 | 2018 |
| 2 | 2018 |
| 3 | 2018 |
| 4 | 2018 |
| 5 | 2018 |
| 6 | 2018 |
| 7 | 2018 |
| 8 | 2018 |
答案 1 :(得分:0)
这是一个存储过程,可以为您提供所需的输出。它比您尝试的时间更长但更简单。
DELIMITER //
DROP PROCEDURE IF EXISTS Calendar //
CREATE PROCEDURE Calendar ()
BEGIN
DECLARE month INT DEFAULT MONTH(CURDATE());
DECLARE year INT DEFAULT YEAR(CURDATE()) - 1;
CREATE TEMPORARY TABLE CAL (month INT, year INT);
REPEAT
SET month = month + 1;
IF (month = 13) THEN
SET month = 1;
SET year = year + 1;
END IF;
INSERT INTO CAL VALUES (month, year);
UNTIL month = MONTH(CURDATE())
END REPEAT;
SELECT * FROM CAL;
DROP TABLE CAL;
END //
DELIMITER ;
CALL Calendar();
输出:
month year
10 2017
11 2017
12 2017
1 2018
2 2018
3 2018
4 2018
5 2018
6 2018
7 2018
8 2018
9 2018