msyql日期用法

知识:

now():当前具体的时间和日期

curdate():当前日期

curtime():当前时间

1.mysql加减时间间隔

设置当前日期变量

set @dt = now();      //设置当前日期

select @dt;    //查询变量值

 加减时间间隔函数date_add()与date_sub()

格式:date_add('某个日期时间',inteval1时间种类)

ps:

select date_add(@dt, interval 1 year);    //加1年

select date_add(@dt, interval 1 month);    //加1月

quarter:季,week:周,day:天,hour:小时,minuter:分钟,second:秒,microsecond:毫秒

注:也可以不用变量,直接加减某个时间,如:select date_add('1998-01-01', interval 1 day);

2.日期相减

datediff(date1,date2):两个日期相减,date1减去date2得到相减之后的天数

ps:select datediff(合同截止日期,now())as '到期提醒天数' from ex_shenzhenquansuyouyi_86 where '到期提醒天数' BETWEEN 0 AND 30

ps:select count(1) as total from ex_shenzhenquansuyouyi_86

ps:select count(1) as abnormal from ex_shenzhenquansuyouyi_86 where datediff(合同截止日期,now()) BETWEEN 0 and 30 and 合同状态<>'已结束'

MySQL中对当前的时间函数now()的操作

格式:select 函数

1.当前年月日时分秒

2.当前日期

select curdate()

 3.当前时分秒

select curtime()

4.当前年份

select year(now())

5.已知年龄(sage)查询出生日期

格式:select year(now())-sage

ps:select year(now())-24

原文地址:https://www.cnblogs.com/zgqbky/p/13447493.html