Mysql GREATEST
From w3cyberlearnings
Contents |
MySQL GREATEST Function
With two or more arguments, and it returns the largest value.
Syntax GREATEST
- Va1: value 1
- Va2: value 2
- Va3: value 3
GREATEST(Va1,Va2,Va3...);
Example 1
mysql> SELECT GREATEST(30,32); +-----------------+ | GREATEST(30,32) | +-----------------+ | 32 | +-----------------+ 1 row in set (0.00 sec) mysql> SELECT GREATEST(30,32,40,200); +------------------------+ | GREATEST(30,32,40,200) | +------------------------+ | 200 | +------------------------+ 1 row in set (0.00 sec)
Example 2
mysql> SELECT GREATEST('a','b','z'); +-----------------------+ | GREATEST('a','b','z') | +-----------------------+ | z | +-----------------------+ 1 row in set (0.00 sec)
Example 3
mysql> SELECT * FROM course; +-----------+------------+--------+ | course_id | title | credit | +-----------+------------+--------+ | 1 | Math100 | 3 | | 2 | Math200 | 3 | | 3 | Math300 | 2 | | 4 | English100 | 2 | | 5 | English200 | 3 | | 6 | English300 | 3 | | 7 | Physic100 | 3 | | 8 | Physic200 | 3 | | 9 | Scien100 | 4 | +-----------+------------+--------+ 9 rows in set (0.00 sec) mysql> SELECT * FROM course -> WHERE credit=GREATEST(3,5,4,1); Empty set (0.00 sec) mysql> SELECT * FROM course -> WHERE credit=GREATEST(1,4,2); +-----------+----------+--------+ | course_id | title | credit | +-----------+----------+--------+ | 9 | Scien100 | 4 | +-----------+----------+--------+ 1 row in set (0.00 sec)