Home > Database > Mysql Tutorial > How to use arithmetic operators (+, -, *, /) with unit values ​​of INTERVAL keyword in MySQL?

How to use arithmetic operators (+, -, *, /) with unit values ​​of INTERVAL keyword in MySQL?

WBOY
Release: 2023-08-31 15:29:02
forward
917 people have browsed it

如何在 MySQL 中将算术运算符(+、-、*、/)与 INTERVAL 关键字的单位值一起使用?

We can use arithmetic operators ( , -, *, /) with the unit value of the INTERVAL keyword, as follows -

Use addition ( )

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)
Copy after login

Use subtraction (-)

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)
Copy after login

Use multiplication (*)

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)
Copy after login

Use division sign (/)

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)
Copy after login

The above is the detailed content of How to use arithmetic operators (+, -, *, /) with unit values ​​of INTERVAL keyword in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template