首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >MySQL 8用一句select打印日历

MySQL 8用一句select打印日历

作者头像
用户1148526
发布2022-04-13 18:10:38
发布2022-04-13 18:10:38
4810
举报
文章被收录于专栏:Hadoop数据仓库Hadoop数据仓库
代码语言:javascript
复制
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;

结果:

加上月分割线的版本:

代码语言:javascript
复制
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;

结果:

代码语言:javascript
复制
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| 月份        | 年中周      | 月中周      | 星期天      | 星期一      | 星期二      | 星期三      | 星期四      | 星期五      | 星期六      |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| 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打印日历

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022/02/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档