• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

MySQL 日期加减函数汇总

mysql 搞代码 4年前 (2022-01-09) 22次浏览 已收录 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 da<a>本文来源gao*daima.com搞@代#码&网6</a>te_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教程》

以上就是MySQL 日期加减函数汇总的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MySQL 日期加减函数汇总

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址