w3resource
MYSQL Tutorial

MySQL INTERVAL() function

INTERVAL() function

MySQL INTERVAL() function returns the index of the argument that is less than the first argument.

Syntax:

INTERVAL(N,N1,N2,N3,...)

It returns 0 if 1st number is less than the 2nd number and 1 if 1st number is less than the 3rd number and so on or -1 if 1st number is NULL.

All arguments are treated as an integer.

MySQL Version: 5.6

Example: MySQL INTERVAL() function

The following MySQL statement the 1st number is 85, which is less than 175. All other numbers in the argument are smaller than 85. So, it returns the index of 175. Notice that indexing is started with the 2nd number and first position is zero.

Code:

SELECT INTERVAL(85, 1, 75, 17, 30,56, 175);

Sample Output:

mysql> SELECT INTERVAL(85, 1, 75, 17, 30,56, 175);
+-------------------------------------+
| INTERVAL(85, 1, 75, 17, 30,56, 175) |
+-------------------------------------+
|                                   5 | 
+-------------------------------------+
1 row in set (0.02 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-interval-function- php MySQL examples | w3resource</title>
<meta name="description" content="example-interval-function- php MySQL examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Select interval of 85, 1, 75, 17, 30,56, 175 using MySQL:</h2>
<table class='table table-bordered'>
<tr>
<th>INTERVAL(85, 1, 75, 17, 30,56, 175)</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("MySQL:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT INTERVAL(85, 1, 75, 17, 30,56, 175)') as $row) {
echo "<tr>";
echo "<td>" . $row['INTERVAL(85, 1, 75, 17, 30,56, 175)'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

JSP script

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-interval-function</title>
</head>
<body>
<%
try {
Class.forName("com.MySQL.jdbc.Driver").newInstance();
String Host = "jdbc:MySQL://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data ="SELECT INTERVAL(85, 1, 75, 17, 30,56, 175)";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>INTERVAL(85, 1, 75, 17, 30,56, 175)</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("INTERVAL(85, 1, 75, 17, 30,56, 175)")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Slideshow of MySQL Comparison Function and Operators

MySQL Comparison Function and Operators, slide presentation

Previous: IN()
Next: IS NOT NULL