MySQL Create Interval
To create an interval in MySQL, use the syntax as shown below:
We start with the interval keyword, followed by the expression and unit parameters. The expression represents the value while the unit specifies the unit format.
For example, we can create 36 hours interval as shown:
Once we have defined an interval, we can use it to perform basic date and time arithmetic operations.
Example 1
For example, let us add 36 hours to the current timestamp:
The above should add 36 hours to the current timestamp. This should return:
Example 2
You can also subtract specific units from time as shown in the example:
The above query subtracts ten days from the specified date. This should return:
Example 3
You can also subtract an interval from an interval as shown below:
This should return:
Example 4
Apart from addition and subtraction, you can perform other arithmetic operations as shown below:
The above will take one day and multiply by 30. This should return 30 days as:
Example 5
You can also perform interval division as shown:
The above query takes one hour and divides by 60. therefore, it should return 1 minute as
Closing
In this article, we covered the interval operator in MySQL and PostgreSQL. It allows us to perform arithmetic operations on date and time.
Keep coding!!