Jump to: navigation, search

Mysql LEAST

From w3cyberlearnings

Contents

MySQL LEAST Function

This functions compares two or more arguments and returns the smallest argument.

Syntax LEAST

  • val1: argument 1
  • val2: argument 2
  • val3: argument 3
LEAST(val1, val2, val3...);

Note

  • 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.

Example 1

mysql> SELECT LEAST(30,40,20,40,3);
+----------------------+
| LEAST(30,40,20,40,3) |
+----------------------+
|                    3 |
+----------------------+
1 row in set (0.00 sec)

mysql> SELECT LEAST('a','b','d','z','g');
+----------------------------+
| LEAST('a','b','d','z','g') |
+----------------------------+
| a                          |
+----------------------------+
1 row in set (0.00 sec)

Example 2

mysql> SELECT * FROM student;
+------------+------+------+
| student_id | name | age  |
+------------+------+------+
|          1 | John |   28 |
|          2 | Bob  |   30 |
|          3 | Kat  |   28 |
|          4 | Pher |   24 |
|          5 | Jim  |   21 |
+------------+------+------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM student 
    -> WHERE age=LEAST(32,33,35,21);
+------------+------+------+
| student_id | name | age  |
+------------+------+------+
|          5 | Jim  |   21 |
+------------+------+------+
1 row in set (0.00 sec)

Related Links



Navigation
Web
SQL
MISC
References