温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL日期加减函数怎么用?

发布时间:2020-05-20 09:14:00 来源:亿速云 阅读:274 作者:Leah 栏目:MySQL数据库

今天小编就为大家带来一篇介绍MySQL日期加减函数的文章。小编觉得挺实用的,为此分享给大家做个参考。一起跟随小编过来看看吧。

1. addtime()  

为日期加上指定秒数

select addtime(now(),1); -- 加1秒

2. adddate()  

有两种用法,第二个参数直接填数字的话是为日期加上指定天数,填interval的话是为日期加上指定的interval时间

select adddate(now(),1); -- 加1天
select adddate(now(), interval 1 day); -- 加1天
select adddate(now(), interval 1 hour); --加1小时
select adddate(now(), interval 1 minute); -- 加1分钟
select adddate(now(), interval 1 second); -- 加1秒
select adddate(now(), interval 1 microsecond); -- 加1毫秒
select adddate(now(), interval 1 week); -- 加1周
select adddate(now(), interval 1 month); -- 加1月
select adddate(now(), interval 1 quarter); -- 加1季
select adddate(now(), interval 1 year); -- 加1年

3. date_add()  

为日期增加一个时间间隔,这个只能使用interval时间作为参数,用法和adddate()一致

select date_add(now(), interval 1 day); -- 加1天
select date_add(now(), interval 1 hour); -- 加1小时
select date_add(now(), interval 1 minute); -- 加1分钟
select date_add(now(), interval 1 second); -- 加1秒
select date_add(now(), interval 1 microsecond); -- 加1毫秒
select date_add(now(), interval 1 week); -- 加1周
select date_add(now(), interval 1 month); -- 加1月
select date_add(now(), interval 1 quarter); -- 加1季
select date_add(now(), interval 1 year); -- 加1年

4. subtime()  

为日期减去指定秒数

select subtime(now(), 1); -- 减1秒

5. subdate()  

与adddate()函数用法一致,有两种用法,第二个参数直接填数字的话是为日期减去指定天数,填interval的话是为日期减去指定的interval时间

select subdate(now(),1); -- 减1天
select subdate(now(), interval 1 day); -- 减1天
select subdate(now(), interval 1 hour); --减1小时
select subdate(now(), interval 1 minute); -- 减1分钟
select subdate(now(), interval 1 second); -- 减1秒
select subdate(now(), interval 1 microsecond); -- 减1毫秒
select subdate(now(), interval 1 week); -- 减1周
select subdate(now(), interval 1 month); -- 减1月
select subdate(now(), interval 1 quarter); -- 减1季
select subdate(now(), interval 1 year); -- 减1年

6. date_sub()   

与date_add()函数用法一致,为日期减去一个时间间隔,这个只能使用interval时间作为参数

select date_sub(now(), interval 1 day); -- 减1天
select date_sub(now(), interval 1 hour); --减1小时
select date_sub(now(), interval 1 minute); -- 减1分钟
select date_sub(now(), interval 1 second); -- 减1秒
select date_sub(now(), interval 1 microsecond); -- 减1毫秒
select date_sub(now(), interval 1 week); -- 减1周
select date_sub(now(), interval 1 month); -- 减1月
select date_sub(now(), interval 1 quarter); -- 减1季
select date_sub(now(), interval 1 year); -- 减1年

关于MySQL日期加减函数就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果喜欢这篇文章,不如把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI