您的位置:首頁技術文章
文章詳情頁

MySQL日期加減函數詳解

瀏覽:26日期:2023-10-14 18:36:13

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日期加減函數詳解的文章就介紹到這了,更多相關MySQL日期加減函數匯總內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: MySQL 數據庫
相關文章:
国产综合久久一区二区三区