如何在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)

Learn MySQL in-depth with real-world projects through our MySQL certification course. Enroll and become a certified expert to boost your career.

乘法(*)的使用

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)

更新日期:2020-06-20

154 次查看

开启你的 职业生涯

完成课程获取认证

开始
广告