MySQL 8用一句select打印日历

Posted wzy0623

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 8用一句select打印日历相关的知识,希望对你有一定的参考价值。

set @a=2022;
set lc_time_names='zh_cn';
select case when (new_yweek = min(new_yweek) over(partition by mon order by new_yweek)) then mon_name else '' end '月份',
       new_yweek '年中周',
       row_number() over(partition by mon order by new_yweek) '月中周',
       cast(ifnull(sum(case when wday = 1 then mday else null end),'') as char(2)) as '星期天',
       cast(ifnull(sum(case when wday = 2 then mday else null end),'') as char(2)) as '星期一',
       cast(ifnull(sum(case when wday = 3 then mday else null end),'') as char(2)) as '星期二',
       cast(ifnull(sum(case when wday = 4 then mday else null end),'') as char(2)) as '星期三',
       cast(ifnull(sum(case when wday = 5 then mday else null end),'') as char(2)) as '星期四',
       cast(ifnull(sum(case when wday = 6 then mday else null end),'') as char(2)) as '星期五',
       cast(ifnull(sum(case when wday = 7 then mday else null end),'') as char(2)) as '星期六'
  from (select everyday, 
               month(everyday) mon, 
               monthname(everyday) mon_name, 
               floor((dayofmonth(everyday)-1)/7)+1 mweek, 
               week(everyday) + 1 new_yweek,
               dayofweek(everyday) wday, dayofmonth(everyday) mday
          from (with recursive tab1(lv) as (
                     select 1 lv 
                      union all
                     select t1.lv + 1 from tab1 t1 where lv < datediff(concat(@a,'-12-31'), concat(@a,'-01-01')) + 1 )
                select date_add(concat(@a,'-01-01'), interval t1.lv - 1 day) everyday
                  from tab1 t1) tt) tt
 group by mon, mon_name, new_yweek;

结果:

加上月分割线的版本:

set @a=2022;
set lc_time_names='zh_cn';
select case when (c2 = min(c2) over(partition by c1)) then c1
            when (c2 = 99) then '----------'
            else '' 
        end '月份',
       case when c2=99 then '----------' else c2 end '年中周',
       c3 '月中周',c4 '星期天',c5 '星期一',c6 '星期二',c7 '星期三',c8 '星期四',c9 '星期五',c10 '星期六'
  from (
select mon c1,
       new_yweek c2,
       row_number() over(partition by mon order by new_yweek) c3,
       cast(ifnull(sum(case when wday = 1 then mday else null end),'') as char(2)) c4,
       cast(ifnull(sum(case when wday = 2 then mday else null end),'') as char(2)) c5,
       cast(ifnull(sum(case when wday = 3 then mday else null end),'') as char(2)) c6,
       cast(ifnull(sum(case when wday = 4 then mday else null end),'') as char(2)) c7,
       cast(ifnull(sum(case when wday = 5 then mday else null end),'') as char(2)) c8,
       cast(ifnull(sum(case when wday = 6 then mday else null end),'') as char(2)) c9,
       cast(ifnull(sum(case when wday = 7 then mday else null end),'') as char(2)) c10
  from (select everyday, 
               month(everyday) mon, 
               monthname(everyday) mon_name, 
               floor((dayofmonth(everyday)-1)/7)+1 mweek, 
               week(everyday) + 1 new_yweek,
               dayofweek(everyday) wday, dayofmonth(everyday) mday
          from (with recursive tab1(lv) as (
                     select 1 lv 
                      union all
                     select t1.lv + 1 from tab1 t1 where lv < datediff(concat(@a,'-12-31'), concat(@a,'-01-01')) + 1 )
                select date_add(concat(@a,'-01-01'), interval t1.lv - 1 day) everyday
                  from tab1 t1) tt) tt
 group by mon, mon_name, new_yweek
 
 union all
 
select * from (
 with recursive tab1(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10) as (
      select 1 c1, 99 c2,'----------' c3,'----------' c4,'----------' c5,'----------' c6,'----------' c7,'----------' c8,'----------' c19,'----------' c10
       union all
      select t1.c1 + 1,99,'----------','----------','----------','----------','----------','----------','----------','----------' from tab1 t1 where c1 < 11 )
 select * from tab1) t)  t order by c1,c2;

结果:

+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| 月份        | 年中周      | 月中周      | 星期天      | 星期一      | 星期二      | 星期三      | 星期四      | 星期五      | 星期六      |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| 1          | 1          | 1          |            |            |            |            |            |            | 1          |
|            | 2          | 2          | 2          | 3          | 4          | 5          | 6          | 7          | 8          |
|            | 3          | 3          | 9          | 10         | 11         | 12         | 13         | 14         | 15         |
|            | 4          | 4          | 16         | 17         | 18         | 19         | 20         | 21         | 22         |
|            | 5          | 5          | 23         | 24         | 25         | 26         | 27         | 28         | 29         |
|            | 6          | 6          | 30         | 31         |            |            |            |            |            |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 2          | 6          | 1          |            |            | 1          | 2          | 3          | 4          | 5          |
|            | 7          | 2          | 6          | 7          | 8          | 9          | 10         | 11         | 12         |
|            | 8          | 3          | 13         | 14         | 15         | 16         | 17         | 18         | 19         |
|            | 9          | 4          | 20         | 21         | 22         | 23         | 24         | 25         | 26         |
|            | 10         | 5          | 27         | 28         |            |            |            |            |            |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 3          | 10         | 1          |            |            | 1          | 2          | 3          | 4          | 5          |
|            | 11         | 2          | 6          | 7          | 8          | 9          | 10         | 11         | 12         |
|            | 12         | 3          | 13         | 14         | 15         | 16         | 17         | 18         | 19         |
|            | 13         | 4          | 20         | 21         | 22         | 23         | 24         | 25         | 26         |
|            | 14         | 5          | 27         | 28         | 29         | 30         | 31         |            |            |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 4          | 14         | 1          |            |            |            |            |            | 1          | 2          |
|            | 15         | 2          | 3          | 4          | 5          | 6          | 7          | 8          | 9          |
|            | 16         | 3          | 10         | 11         | 12         | 13         | 14         | 15         | 16         |
|            | 17         | 4          | 17         | 18         | 19         | 20         | 21         | 22         | 23         |
|            | 18         | 5          | 24         | 25         | 26         | 27         | 28         | 29         | 30         |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 5          | 19         | 1          | 1          | 2          | 3          | 4          | 5          | 6          | 7          |
|            | 20         | 2          | 8          | 9          | 10         | 11         | 12         | 13         | 14         |
|            | 21         | 3          | 15         | 16         | 17         | 18         | 19         | 20         | 21         |
|            | 22         | 4          | 22         | 23         | 24         | 25         | 26         | 27         | 28         |
|            | 23         | 5          | 29         | 30         | 31         |            |            |            |            |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 6          | 23         | 1          |            |            |            | 1          | 2          | 3          | 4          |
|            | 24         | 2          | 5          | 6          | 7          | 8          | 9          | 10         | 11         |
|            | 25         | 3          | 12         | 13         | 14         | 15         | 16         | 17         | 18         |
|            | 26         | 4          | 19         | 20         | 21         | 22         | 23         | 24         | 25         |
|            | 27         | 5          | 26         | 27         | 28         | 29         | 30         |            |            |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 7          | 27         | 1          |            |            |            |            |            | 1          | 2          |
|            | 28         | 2          | 3          | 4          | 5          | 6          | 7          | 8          | 9          |
|            | 29         | 3          | 10         | 11         | 12         | 13         | 14         | 15         | 16         |
|            | 30         | 4          | 17         | 18         | 19         | 20         | 21         | 22         | 23         |
|            | 31         | 5          | 24         | 25         | 26         | 27         | 28         | 29         | 30         |
|            | 32         | 6          | 31         |            |            |            |            |            |            |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 8          | 32         | 1          |            | 1          | 2          | 3          | 4          | 5          | 6          |
|            | 33         | 2          | 7          | 8          | 9          | 10         | 11         | 12         | 13         |
|            | 34         | 3          | 14         | 15         | 16         | 17         | 18         | 19         | 20         |
|            | 35         | 4          | 21         | 22         | 23         | 24         | 25         | 26         | 27         |
|            | 36         | 5          | 28         | 29         | 30         | 31         |            |            |            |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 9          | 36         | 1          |            |            |            |            | 1          | 2          | 3          |
|            | 37         | 2          | 4          | 5          | 6          | 7          | 8          | 9          | 10         |
|            | 38         | 3          | 11         | 12         | 13         | 14         | 15         | 16         | 17         |
|            | 39         | 4          | 18         | 19         | 20         | 21         | 22         | 23         | 24         |
|            | 40         | 5          | 25         | 26         | 27         | 28         | 29         | 30         |            |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 10         | 40         | 1          |            |            |            |            |            |            | 1          |
|            | 41         | 2          | 2          | 3          | 4          | 5          | 6          | 7          | 8          |
|            | 42         | 3          | 9          | 10         | 11         | 12         | 13         | 14         | 15         |
|            | 43         | 4          | 16         | 17         | 18         | 19         | 20         | 21         | 22         |
|            | 44         | 5          | 23         | 24         | 25         | 26         | 27         | 28         | 29         |
|            | 45         | 6          | 30         | 31         |            |            |            |            |            |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 11         | 45         | 1          |            |            | 1          | 2          | 3          | 4          | 5          |
|            | 46         | 2          | 6          | 7          | 8          | 9          | 10         | 11         | 12         |
|            | 47         | 3          | 13         | 14         | 15         | 16         | 17         | 18         | 19         |
|            | 48         | 4          | 20         | 21         | 22         | 23         | 24         | 25         | 26         |
|            | 49         | 5          | 27         | 28         | 29         | 30         |            |            |            |
| ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- | ---------- |
| 12         | 49         | 1          |            |            |            |            | 1          | 2          | 3          |
|            | 50         | 2          | 4          | 5          | 6          | 7          | 8          | 9          | 10         |
|            | 51         | 3          | 11         | 12         | 13         | 14         | 15         | 16         | 17         |
|            | 52         | 4          | 18         | 19         | 20         | 21         | 22         | 23         | 24         |
|            | 53         | 5          | 25         | 26         | 27         | 28         | 29         | 30         | 31         |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
74 rows in set (0.00 sec)

参考:Oracle用SQL打印日历

以上是关于MySQL 8用一句select打印日历的主要内容,如果未能解决你的问题,请参考以下文章

一句日历2019年8月

从 MySQL 获取查询并将其打印到 javascript 日历中

PHP:检测重复列结果的结束(日历日)

用一句SQL取出第 m 条到第 n 条记录的方法

MySQL使用伪列

python打印日历