Tags
MySQL
Asked 6 years ago
2 Oct 2018
Views 1097
sarah

sarah posted

mysql least have starge behaviour

select least(1,2,'a')

i am getting result 0 instead of 1 because 1 is least from the given value
sec8

sec8
answered Apr 25 '23 00:00

The LEAST function in MySQL returns the smallest value among the provided arguments. If any of the arguments is NULL, the function returns NULL.

Here's an example code snippet to illustrate the behavior of the LEAST function in MySQL:



-- Define the arguments to find the least value
SELECT LEAST(4, 7, 2);

-- Outputs 2, since 2 is the smallest value among the arguments

-- Define the arguments with NULL values
SELECT LEAST(1, NULL, 2);

-- Outputs NULL, since one of the arguments is NULL

-- Define all NULL arguments
SELECT LEAST(NULL, NULL, NULL);

-- Outputs NULL, since all arguments are NULL

In the first example, the LEAST function returns the smallest value among the provided arguments. In the second example, one of the arguments is NULL, so the function returns NULL. In the third example, all arguments are NULL, so the function also returns NULL.

It's also important to note that the behavior of the LEAST function is deterministic, meaning that it always returns the same result given the same input. If multiple values have the same minimum value, the function returns the first occurrence of that value in the argument list
Post Answer