How can I use MySQL INTERVAL() function with a column of a table?


We can use INTERVAL() function with a column of a table by providing the first argument as the name of the column. In this case, al the values in that column would be compared with the values given as the other arguments of INTERVAL() function and on that basis of comparison, the result set is provided. To understand it, the data from the employee table is used which is as follows −

mysql> Select* from employee568;
+----+--------+--------+
| ID | Name   | Salary |
+----+--------+--------+
| 1  | Gaurav | 50000  |
| 2  | Rahul  | 20000  |
| 3  | Advik  | 25000  |
| 4  | Aarav  | 65000  |
| 5  | Ram    | 20000  |
| 6  | Mohan  | 30000  |
| 7  | Aryan  |  NULL  |
| 8  | Vinay  |  NULL  |
+----+--------+--------+
8 rows in set (0.00 sec)

mysql> Select * from employee WHERE INTERVAL(ID,4) > 0;
+----+-------+--------+
| ID | Name  | Salary |
+----+-------+--------+
| 4  | Aarav | 65000  |
| 5  | Ram   | 20000  |
| 6  | Mohan | 30000  |
| 7  | Aryan |  NULL  |
| 8  | Vinay |  NULL  |
+----+-------+--------+
5 rows in set (0.00 sec)

The above result set has 5 rows of ID = 4 to 8 as the INTERVAL function has greater than 0 values for these rows.

Swarali Sree
Swarali Sree

I love thought experiments.

Updated on: 22-Jun-2020

297 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements