由于MySQL / SQL查询,我需要一个2列12行的表:
表应类似于:
我尝试过:
DELIMITER $$
DROP PROCEDURE IF EXISTS my_loop$$
CREATE PROCEDURE my_loop()
BEGIN
DECLARE x INT;
SET x = 1;
WHILE x <= 12 DO
SELECT monthname(SUBDATE("2019-12-01", INTERVAL x month)) as months, day(LAST_DAY(SUBDATE("2019-12-01", INTERVAL x month))) as days;
SET x = x + 1;
END WHILE;
END$$
DELIMITER ;
call my_loop();
但是它只打印出第一个迭代:
无论有没有迭代如何解决?
答案 0 :(得分:0)
您可以通过查询执行此操作
select monthname(makedate(year(curdate()), 1) + interval (x.mon - 1) month) as month_name,
day(last_day(makedate(year(curdate()), 1) + interval (x.mon - 1) month)) as days_in_month
from (select 1 as mon union all select 2 union all select 3 union all select 4 union all
select 5 as mon union all select 6 union all select 7 union all select 8 union all
select 9 as mon union all select 10 union all select 11 union all select 12
) x
order by x.mon;
Here是db <>小提琴。