MySQL can perform date arithmetic using the addition and subtraction operators by adding the INTERVAL keyword to a time unit, date, or datetime.
Add 2 days to a specific date.
mysql> Select '2017-05-20' + INTERVAL 2 day; +-------------------------------+ | '2017-05-20' + INTERVAL 2 day | +-------------------------------+ | 2017-05-22 | +-------------------------------+ 1 row in set (0.00 sec)
Subtract 2 days from a specific date.
mysql> Select '2017-05-20' - INTERVAL 2 day; +-------------------------------+ | '2017-05-20' - INTERVAL 2 day | +-------------------------------+ | 2017-05-18 | +-------------------------------+ 1 row in set (0.00 sec)
Add 2 hours of time.
mysql> Select '2017-05-20 05:04:35' + INTERVAL 3 hour; +-----------------------------------------+ | '2017-05-20 05:04:35' + INTERVAL 3 hour | +-----------------------------------------+ | 2017-05-20 08:04:35 | +-----------------------------------------+ 1 row in set (0.00 sec)
Add one month to a specific date
mysql> Select '2017-05-20 05:04:35' + INTERVAL 1 month; +------------------------------------------+ | '2017-05-20 05:04:35' + INTERVAL 1 month | +------------------------------------------+ | 2017-06-20 05:04:35 | +------------------------------------------+ 1 row in set (0.00 sec)
In this way, with the INTERVAL keyword, we can perform date operations.
The above is the detailed content of How does MySQL use addition and subtraction operators for date operations?. For more information, please follow other related articles on the PHP Chinese website!