w3resource logo


Mysql LEAST function

MySQL LEAST() function

rating MySQL LEAST() function has average rating 7 out of 10. Total 6 users rated.

<<PreviousNext>>

Description

MySQL LEAST() function returns the smallest argument from two or more arguments.

Syntax

LEAST(value1,value2,...)

The arguments are compared using the following rules:

  • If any argument is NULL, the result is NULL. No comparison is needed.
  • If the return value is used in an INTEGER context or all arguments are integer-valued, they are compared as integers.
  • If the return value is used in a REAL context or all arguments are real-valued, they are compared as reals.
  • If the arguments comprise a mix of numbers and strings, they are compared as numbers.
  • If any argument is a nonbinary (character) string, the arguments are compared as nonbinary strings.
  • In all other cases, the arguments are compared as binary strings.

MySQL Version : 5.6

Example : MySQL LEAST() function

The following MySQL statement will find the smallest out of the list of arguments.

SELECT LEAST(15,10,25);

Output

MYSQL LEAST

Example : MySQL LEAST() function using string

The following MySQL statement will find the smallest out of the list of arguments. It returns M, since S and Z come after M.

SELECT LEAST("Z","M","S");

Output

MYSQL LEAST EXAMPLE

Example : MySQL LEAST() function with where clause

The following MySQL statement will fetch those books from book_mast table which have less number of pages than lowest argument returning from the LEAST() function.

SELECT book_name,dt_of_pub,no_page
FROM book_mast
WHERE no_page<LEAST(500,300,395); 

Sample table : book_mast

Output

MYSQL LEAST EXAMPLE1

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example2-least- php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>List of books whose no. of pages are 300, along with their date of publish and number of pages :</h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='250' align='center'>Book</td><td width='250' align='center'>Date of publish</td><td width='250' align='center'>Number of pages</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT book_name,dt_of_pub,no_page
FROM book_mast
WHERE no_page<LEAST(500,300,395)");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['book_name'] . "</td>";
echo "<td align='center' width='200'>" . $row['dt_of_pub'] . "</td>";
echo "<td align='center' width='200'>" . $row['no_page'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser



We have compiled a number of MySQL Questions and Answers. Check and assess you MySQL skill by visiting those questions and answers.

<<PreviousNext>>