一, 时间函数
基礎語法
1,獲取當前時間
SELECT NOW(),CURDATE(),CURTIME()
SELECT DATE_ADD(CURDATE(),INTERVAL 1 DAY);
SELECT DATE_ADD('2016-08-01',INTERVAL 1 DAY);
SELECT DATE_SUB(CURDATE(),INTERVAL 1 DAY);
SELECT DATE_SUB('2016-08-01',INTERVAL 1 DAY);
SELECT DATEDIFF('2020-08-02 00','2020-08-01 00');
SELECT STR_TO_DATE('2020-08-01 00', '%Y-%m-%d %H')
1,獲取本月第一天,最後一天
select last_day(curdate());
select DATE_ADD(curdate(),interval -day(curdate())+1 day);
2,兩個日期加減
SELECT (UNIX_TIMESTAMP('2020-08-18 09:30:00')-UNIX_TIMESTAMP('2020-08-18 08:00:00'))/60/60
3,时间函数套用
SELECT DATE_ADD(CURDATE(), INTERVAL(CAST(help_topic_id AS SIGNED INTEGER)- IF(DAY(CURDATE())>25, DAY(CURDATE())-25 , DAY(LAST_DAY(DATE_ADD(CURDATE(),INTERVAL(-1) MONTH ) ) ) -25 + DAY(CURDATE()) -1 ) ) DAY) EVERYDAY
FROM mysql.help_topic
WHERE help_topic_id < (IF(DAY(CURDATE())>25, DAY(CURDATE())-25 , DAY(LAST_DAY(DATE_ADD(CURDATE(),INTERVAL(-1) MONTH ) ) ) -25 + DAY(CURDATE()) -1 ) )
ORDER BY help_topic_id;;