mabatis查询时间格式_mysql weekday(date)/subdate(date,间隔天数)查询年龄/本月/周过生日...

表信息

edd5d870524c85b827946013aff0fbe7.png

准备知识

select now(); #当前日期时间

select curdate (); #当前日期

select curtime(); #当前时间

weekday(date) #返回日期的星期索引(0=周一,1=周二,,,6=周日)

subdate(date,间隔天数) #从指定日前减去几天 注意:当第二个参数为负数时,减去负数相当于加,这时表示从指定日期加上几天。

date_format(日期时间,格式) 详细日期格式设置见 MySQL DATE_FORMAT() 函数

当前日期时间:

78cc88738ce7e327d0e45cefe9721b56.png

1 -- 查询各学生的年龄

select s_name, year(curdate())-year(s_age) as 年龄 from student group by s_name;

2 -- 查询本周过生日的学生 between... and

select s_name from student 
where date_format(s_age, '%m-%d') 
between date_format(subdate(curdate(),weekday(curdate())),'%m-%d')  and date_format(subdate(curdate(),weekday(curdate())-6),'%m-%d');

3 -- 查询下周过生日的学生

select s_name from student 
where date_format(s_age, '%m-%d') 
between date_format(subdate(curdate(),weekday(curdate())-7),'%m-%d')  and date_format(subdate(curdate(),weekday(curdate())-13),'%m-%d');

4 -- 查询本月过生日的学生

select s_name from student where month(s_age) = month(curdate());

5 --查询下月过生日的学生

select s_name from student where month(s_age) =case when  month(curdate())  + 1>12 then 1 else month(curdate()) + 1 end;

#注意:判断当月份为12月的时候,下月为1月。

# 总结一下case when 的用法:

case when (条件) then (结果1) else (结果2) end;

相比较excel的if()函数:

=if (条件,结果1,结果2)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值