MySQL日期加减函数详解

所属分类: 数据库 / Mysql 阅读数: 390
收藏 0 赞 0 分享

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 found_row()使用详解

在参考手册中对found_rows函数的描述是: it is desirable to know how many rows the statement would have returned without the LIMIT. 也就是说,它返回值是如果SQL语句没有加LI
收藏 0 赞 0 分享

很全面的MySQL处理重复数据代码

这篇文章主要为大家详细介绍了MySQL处理重复数据的实现代码,如何防止数据表出现重复数据及如何删除数据表中的重复数据,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

图文详解Ubuntu下安装配置Mysql教程

这篇文章主要以图文结合的方式详细为大家介绍了Ubuntu安装配置Mysql的实现步骤,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Ubuntu下mysql安装和操作图文教程

这篇文章主要为大家详细分享了Ubuntu下mysql安装和操作图文教程,喜欢的朋友可以参考一下
收藏 0 赞 0 分享

Linux/UNIX和Window平台上安装Mysql

这篇文章主要为大家详细介绍了Linux/UNIX和Window两个系统上采用命令安装Mysql的方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

忘记MySQL的root密码该怎么办

忘记密码总是一件令人头疼的事情,当我们忘记了MySQL的root密码该怎么办?本文给出解决方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Mysql存储引擎MyISAM的常见问题(表损坏、无法访问、磁盘空间不足)

这篇文章主要介绍了Mysql存储引擎MyISAM的常见问题,针对表损坏、无法访问、磁盘空间不足等问题进行解决,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

VS2013连接MySQL5.6成功案例一枚

这篇文章主要为大家分享了VS2013连接MySQL5.6成功案例一枚,很有实用性,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Windows下mysql修改root密码的4种方法

这篇文章主要为大家详细介绍了windows下mysql修改root密码的4种方法,大家可以根据的自己的实际情况进行选择,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

mysql Sort aborted: Out of sort memory, consider increasing server sort buffer size的解决方法

这篇文章主要介绍了mysql Sort aborted: Out of sort memory, consider increasing server sort buffer size的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多