根据已有的日历表,输入月份,查询该月每天属于星期几,结果展示成日历的样式

查询结果如下图:

实现sql如下:

with month as (

select t_date from calendar_table where substr(t_date,1,7) = substr(查询月份,1,7)

),

flag as (

select weekday(concat(substr(查询月份,1,7),‘-01’))  tag

),

week_0 as(

select rownum num, tag from (

select case when 0<flag.tag then 1 else '' end as num, '' tag from flag

union

select rownum num, t_date || char(10) || '查询结果'  tag from month where (weekday(month.t_date)) = 0

) where num is not null

),
week_1 as(

select rownum num, tag from (

select case when 1<flag.tag then 1 else '' end as num, '' tag from flag

union

select rownum num, t_date || char(10) || '查询结果'  tag from month where (weekday(month.t_date)) = 1

) where num is not null

),
week_2 as(

select rownum num, tag from (

select case when 2<flag.tag then 1 else '' end as num, '' tag from flag

union

select rownum num, t_date || char(10) || '查询结果'  tag from month where (weekday(month.t_date)) = 2

) where num is not null

),
week_3 as(

select rownum num, tag from (

select case when 3<flag.tag then 1 else '' end as num, '' tag from flag

union

select rownum num, t_date || char(10) || '查询结果'  tag from month where (weekday(month.t_date)) = 3

) where num is not null

),
week_4 as(

select rownum num, tag from (

select case when 4<flag.tag then 1 else '' end as num, '' tag from flag

union

select rownum num, t_date || char(10) || '查询结果'  tag from month where (weekday(month.t_date)) = 4

) where num is not null

),
week_5 as(

select rownum num, tag from (

select case when 5<flag.tag then 1 else '' end as num, '' tag from flag

union

select rownum num, t_date || char(10) || '查询结果'  tag from month where (weekday(month.t_date)) = 5

) where num is not null

),
week_6 as(

select rownum num, tag from (

select case when 6<flag.tag then 1 else '' end as num, '' tag from flag

union

select rownum num, t_date || char(10) || '查询结果'  tag from month where (weekday(month.t_date)) = 6

) where num is not null

)

select week_0.tag 星期天,week_1.tag 星期一, week_2.tag 星期二, week_3.tag 星期三, week_4.tag 星期四, week_5.tag 星期五, week_6.tag 星期六
from week_0
left join week_1 on week_0.num = week_1.num
left join week_2 on week_0.num = week_2.num
left join week_3 on week_0.num = week_3.num
left join week_4 on week_0.num = week_4.num
left join week_5 on week_0.num = week_5.num
left join week_6 on week_0.num = week_6.num


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是用C语言编写的实现代码(不包含闰年判断): ``` #include <stdio.h> int main(){ int year, month, day, week; printf("请输入年份: "); scanf("%d", &year); printf("==========================================\n"); printf(" %d 年 \n", year); printf("==========================================\n"); printf(" 一 二 三 四 五 六 日\n"); for(month=1; month<=12; month++){ printf("==========================================\n"); switch(month){ case 1: printf("一"); break; case 2: printf("二"); break; case 3: printf("三"); break; case 4: printf("四"); break; case 5: printf("五"); break; case 6: printf("六"); break; case 7: printf("七"); break; case 8: printf("八"); break; case 9: printf("九"); break; case 10: printf("十"); break; case 11: printf("十一"); break; case 12: printf("十二"); break; } printf("\n"); // 计第一天是星期几 day = 1; week = (day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7; // 输出日期 for(int i=0; i<week; i++) printf(" "); for(int i=1; i<=31; i++){ if((i<10 && month==2) || (i<10 && month==4) || (i<10 && month==6) || (i<10 && month==9) || (i<10 && month==11)) printf(" "); if(i>28 && (month==2 || month==4 || month==6 || month==9 || month==11)) break; printf("%2d ", i); week++; if(week%7 == 0) printf("\n"); } printf("\n"); } printf("==========================================\n"); return 0; } ``` 代码实现思路: - 首先让用户输入年份。 - 接着利用循环和switch-case语句,分别打印12个月份日历。 - 在输出具体日期时,先利用蔡勒公式去计第一天是星期几,再利用循环输出每一天对应的日期和星期几。为了便于输出对齐,可以考虑对于每个的前几天,输出空格让其对齐。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

他山之石女士

你一元我一元,是我创作的源泉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值