如何在MySQL中将算术运算符(+,-,*,/)与INTERVAL关键字的单位值一起使用?
我们可以对INTERVAL关键字的单位值使用算术运算符(+,-,*,/),如下所示-
使用加法(+)
mysql> Select date('2017-10-22' + INTERVAL 2+2 Year) AS 'Date After (2+2)Years'; +------------------------+ | Date After (2+2) Years | +------------------------+ | 2021-10-22 | +------------------------+ 1 row in set (0.00 sec)
减法的使用(-)
mysql> Select date('2017-10-22' + INTERVAL 2-2 Year) AS 'Date After (2-2)Years'; +------------------------+ | Date After (2-2) Years | +------------------------+ | 2017-10-22 | +------------------------+ 1 row in set (0.00 sec)
乘法的使用(*)
mysql> Select date('2017-10-22' + INTERVAL 2*2 Year) AS 'Date After (2*2)Years'; +------------------------+ | Date After (2*2) Years | +------------------------+ | 2021-10-22 | +------------------------+ 1 row in set (0.00 sec)
使用除法(/)
mysql> Select date('2017-10-22' + INTERVAL 2/2 Year) AS 'Date After (2/2) Years'; +------------------------+ | Date After (2/2) Years | +------------------------+ | 2018-10-22 | +------------------------+ 1 row in set (0.00 sec)