MYSQL Tutorial

MySQL IN() function

IN() function

MySQL IN() function finds a match in the given arguments.


expr IN (value,...)

The function returns 1 if expr is equal to any of the values in the IN list, otherwise, returns 0. If all values are constants, they are evaluated according to the type of expr and sorted. The search for the item then is done using a binary search. This means IN is very quick if the IN value list consists entirely of constants. Otherwise, type conversion takes place according to the rules.

MySQL Version: 5.6

Example: MySQL IN() function

The following MySQL statement will return 1 because the specified value is within the range of values.


SELECT 10 IN(15,10,25);

Sample Output:

mysql> SELECT 10 IN(15,10,25);
| 10 IN(15,10,25) |
|               1 | 
1 row in set (0.00 sec)

Example : IN() function with where clause

The following MySQL statement checks which books have either 300 or 400 or 500 pages.


SELECT book_name,dt_of_pub,no_page
FROM book_mast          
WHERE no_page IN (300,400,500);

Sample table: book_mast

Sample Output:

mysql> SELECT book_name,dt_of_pub,no_page
    -> FROM book_mast          
    -> WHERE no_page IN (300,400,500);
| book_name                           | dt_of_pub  | no_page |
| Understanding of Steel Construction | 2003-07-15 |     300 | 
| Fundamentals of Thermodynamics      | 2002-10-14 |     400 | 
2 rows in set (0.09 sec)

PHP script

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example1-in-function- php MySQL examples | w3resource</title>
<meta name="description" content="example1-in-function- php MySQL examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>A list of books whose number of pages are either 300 or 400 or 500 along with their date of publish and number of pages:</h2>
<table class='table table-bordered'>
<th>Book</th><th>Date of publish</th><th>Number of pages</th>
$db = "your_dbname";
$dbh = new PDO("MySQL:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT book_name,dt_of_pub,no_page
FROM book_mast
WHERE no_page IN (300,400,500)') as $row) {
echo "<tr>";
echo "<td>" . $row['book_name'] . "</td>";
echo "<td>" . $row['dt_of_pub'] . "</td>";
echo "<td>" . $row['no_page'] . "</td>";
echo "</tr>";

View the example in browser

JSP script

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
try {
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 book_name,dt_of_pub,no_page FROM book_mast WHERE no_page IN (300,400,500)";
rs = statement.executeQuery(Data);
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Date of publish</td>
<td>Number of pages</td>
while (rs.next()) {
<%   }    %>
} catch (Exception ex) {
out.println("Can’t connect to database.");

Slideshow of MySQL Comparison Function and Operators

MySQL Comparison Function and Operators, slide presentation

New Content: Composer: Dependency manager for PHP, R Programming