小編這次要給大家分享的是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如何實現日期加減函數的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。