如何将 INTERVAL 关键字与 MySQL NOW() 和 CURDATE() 函数配合使用?
INTERVAL 关键字与 NOW() 和 CURDATE() MySQL 函数可以类似于用于日期值的 time、date 或 datetime 单位的方式使用。
示例
将 INTERVAL 与 MySQL NOW() 搭配使用
mysql> Select NOW() + INTERVAL 2 day; +------------------------+ | NOW() + INTERVAL 2 day | +------------------------+ | 2017-10-30 09:19:31 | +------------------------+ 1 row in set (0.00 sec) mysql> Select NOW() - INTERVAL 2 day; +------------------------+ | NOW() - INTERVAL 2 day | +------------------------+ | 2017-10-26 09:19:45 | +------------------------+ 1 row in set (0.00 sec) mysql> Select NOW() - INTERVAL 2 hour; +-------------------------+ | NOW() - INTERVAL 2 hour | +-------------------------+ | 2017-10-28 07:19:51 | +-------------------------+ 1 row in set (0.00 sec)
示例
将 INTERVAL 与 MySQL CURDATE() 搭配使用
mysql> Select CURDATE() - INTERVAL 2 day; +----------------------------+ | CURDATE() - INTERVAL 2 day | +----------------------------+ | 2017-10-26 | +----------------------------+ 1 row in set (0.00 sec) mysql> Select CURDATE() + INTERVAL 2 day; +----------------------------+ | CURDATE() + INTERVAL 2 day | +----------------------------+ | 2017-10-30 | +----------------------------+ 1 row in set (0.00 sec)
广告