您的位置:首页 > 数据库 > MySQL

mysql数学函数之greatest,least用法

2018-03-08 11:00 513 查看

语法

GREATEST(value1,value2,…)

With two or more arguments, returns the largest (maximum-valued) argument. The arguments are compared using the same rules as for LEAST().

使用两个或更多参数,返回最大(最大值)参数。 参数使用与LEAST()相同的规则进行比较。

LEAST(value1,value2,…)

With two or more arguments, returns the smallest (minimum-valued) argument. The arguments are compared using the following rules:

o If any argument is NULL, the result is NULL. No comparison is needed.

o If all arguments are integer-valued, they are compared as integers.

o If at least one argument is double precision, they are compared as double-precision values. Otherwise, if at least one argument is a DECIMAL value, they are compared as DECIMAL values.

o If the arguments comprise a mix of numbers and strings, they are compared as numbers.

o If any argument is a nonbinary (character) string, the arguments are compared as nonbinary strings.

o In all other cases, the arguments are compared as binary strings.

The return type of LEAST() is the aggregated type of the comparison argument types.

使用两个或更多参数,返回最小(最小值)参数。 使用以下规则比较参数:

o如果有任一参数为NULL,则结果为NULL。 不需要比较。

o如果所有参数都是整数值,则将它们作为整数进行比较。

o如果至少有一个参数是双精度,则将它们作为双精度值进行比较。 否则,如果至少有一个参数是DECIMAL值,则将它们作为DECIMAL值进行比较。

o如果参数包含数字和字符串的混合,则将它们作为数字进行比较。

o如果有任一参数是非二进制(字符)字符串,则参数将作为非二进制字符串进行比较。

o在所有其他情况下,参数将作为二进制字符串进行比较。

LEAST()的返回类型是比较参数类型的聚合类型。

实例

SELECT greatest(3,1,5,9,2,NULL);     # null
SELECT greatest(3,1,5,9,2,6);     # 9
SELECT greatest(3,1,5,9,2,6.0);     # 9.0
SELECT greatest(3,1,5,9,2,'6');     # 9

SELECT least(3,1,5,9,2,NULL);     # null
SELECT least(3,1,5,9,2,6);     # 1
SELECT least(3,1,5,9,2,6.0);     # 1.0
SELECT least(3,1,5,9,2,'6');     # 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: